Title: 976140 ObjectOriented Programming
1- 976-140 Object-Oriented Programming
- 344-202 Introduction to OOP
- ???????????????????
8. Inheritance
Faculty of Technology and
Environment
Prince of Songkla University, Phuket Campus
2/2551
2Why inheritance?
- How to programming for all characters in the game?
3Why inheritance?
GradStudent Id Name Major thesis register() checkG
rade() listConference()
Staff Id Name Office Salary paySalary() checkDayOf
f()
Lecturer Id Name Office Salary assignCourse() payS
alary() checkDayOff()
Student Id Name Major register() checkGrade() chec
kActivity()
Reuse
4Reuse
- OOP ?????????????????????????????????????????
(reuse) - ???? ?????????????????????????????????????????????
? ??? ???? - ????????????????????????????????????????
???????????????????????? - Reuse ????????
- ????????????? (Inheritance)
- ????????????? (Composition)
5"is a" versus "has a" Relationships
- ??????????????????????? 2 ??? ??????
- ??????????????? is a ?????????????
(Inheritance) - ??????????? subclass ?????????????
- a car is a vehicle
- ??????????????? has a ?????????????
(Composition) - ??????????????????????? attribute
- a car has wheels
6Composition
- ????????????? ????????????????????????????????????
?????? - Example
- public class Address
- String number
- String road
- String city
- String province
- String postcode
-
- public class Person
- String name
- String birthDate
- Address address
-
7Inheritance ?????????
- ????????? (Inheritance) ????????????????????????
OOP ??????????????????????????????????????????????
???? - ???????????????????? ?????????????(attributes)????
?????????(behaviour) ??????????? - ????????????????????????????????? ????????
??????? (parent class / superclass / based
class) - ???????????????????????????? ????????
??????? (child class / subclass / derived class )
8Inheritance class diagram
- A ?????????????? B ??????? B ????????????????????
A
A
???????
Vehicle
???????????????????????????
B
???????
Bicycle
Car
Helicopter
Racing
Sedan
9Inheritance
- ??????? ???????????????? ? ???? ? ?? (general)
- ??????? ??????????????????? ??????????????????????
????????? (specialize) - ????????????????? ?????????????????????? Object
??????????????????? - Attribute ??? method ??????????????????
????????????????????? ???????????????
???????????????????? - ?????????????????? attribute ??? method ???????
- ???????????????????????????????????????????????
10Inheritance
- Redesign the Student classes with inheritance
Student Id Name Major register() checkGrade()
Student
GradStudent thesis listConference()
UnderGradStudent checkActivity()
11Keyword extends
- ????????????????????????????????????????????
???????????????????????????????????? extends - Syntax
- modifier class ??????????? extends
??????????? - ????
- public class GradStudent extends Student
- public class Car extends Vehicle
- public class Manager extends Employee
Employee
Manager
12Ex. InherritanceDemo1
13Ex. InherritanceDemo1
14Keyword extends
Student
15Keyword extends
- ??????????????????????????????????????????????????
?? - ??????????????????????????????????????????????????
????????
16 java.lang.Object
- ?????????? ?????????????????????????? Object
?????????? package java.lang - ????????????????????????????????????????????????
??????????????????????? Object ????????? - ????????????? extends ????????????????????????????
????????? - ??????????????????????????????????????????????????
?
Object
Vehicle
Car
17The methods of the Object class
- Object ????????????????????? ??????????? Object
??????????????????? - ??????????? Object ???????? Java API
(http//java.sun.com) ???? - public String toString() ?????? String ?????????
object
18Ex.
Object
Radio
ToStringDemo
19Keyword null
- ???????????????? object ????????? null ?????
object ????????????????????????????????????? ????
object ????????????????? Garbage Collector - Garbage Collector ?? JVM ?????????????? object
?????????????????????????? object
???????????????????
20Method overriding
- ??????????????????????????????????????????????????
???????????????? ???????? ????? method overriding - Overridden method ??? ?????????????????????????
override - Overriding method ??? ??????????????????????
override - ?????????????????? ????????? ????????????????????
??????????????????????? ??????????????????
21Ex. Method overriding
22Ex. Method overriding
23Ex. Method overriding
24Keyword this
- this ????????? 2 ????
- ??????? 1 ????????????????????????? ??? object
??? this ???????????? reference
?????????????????????????????????????
25Keyword this
- ??????? 2 ??? this ????? constructor ???? ?
??????
26Keyword super
- ??? super ??? 2 ????
- ??????? 1 ??? super ?????????????????????????????
27Keyword super
28Keyword super
- ??????? 2 ???????? constructor
?????????????????? ??????????????????????????????
constructor ????????????
29Find some errors
30Shadowing Data Fields
- ???? ?????????????????????????????????????????????
???? - ???????????????????????????????????????????
?????????????????????????????????????????? - ?????????????????????????????????????? ???????
keyword super
31Shadowing Data Fields
32Shadowing Data Fields
33Keyword final
- Final class ??? class ??????????????? final
???????????????????????????
Compile error!!! InheritanceDemo1.java12 cannot
inherit from final Employee
34Keyword final
- Final method ?????????????? final
??????????????? override ??????
Compile error!!! InheritanceDemo1.java14
printData() in Manager cannot override
printData() in Employee overridden method is
final
35Access modifier
- Access modifier ???????????????????? ??????
???????? ?????????????????????????? ???????? 4
????? - public ???????????????????????????????????
- protected ??????????????????????????????????
package ???????????????????????????? - package ????????????? package ???????????????????
????????????????? - private ????????????????????? private
?????????????????????????????????????????
36Access modifier
- ????????? access modifier
- class
attribute method - public / / /
- protected X / /
- package / / /
- private X / /
- ???????????????????? package ????????????????
?????? ?????????????????????? access modifier
???
37Inheriting attributes and methods
- ????????????????? ?????????????? ??????? private
???????????????????????????
38Inheriting attributes and methods
39Inheriting attributes and methods
- ???????????????????? attributed ??? method
??????????????????????? protected ??? ???????
package??? - ?????????????????????? package ?????????????
package ???????????????? - package a
package b
A
B
C
40Inheriting attributes and methods
41Inheritance and constructor
- ???????? object ??????????????????? default
constructor ????????? ?????????????????????????
constructor ?????????????
42Summary
- ??? reuse class ????? 2 ?????? inheritance
composition - ????????????????????????????????????????????????
- ??????????????????? attribute ??? method ???????
public, protected ??? - ?????????? private ??????
- Method overriding ????????????????????????????????
????? - this ??????????????????????????????????
- super ?????????????????????????????????
- Class ??????? final ??????????????????
- Method ??????? final ????????? overrided ???
- Constructor ???????????????????????????