976140 ObjectOriented Programming - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

976140 ObjectOriented Programming

Description:

Object-Oriented Programming 2/2551. Why inheritance? ... Sedan. Bicycle. Car. Helicopter. ???????. ???????. ???????????????????????????. 9 ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 43
Provided by: wernerv
Category:

less

Transcript and Presenter's Notes

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
2
Why inheritance?
  • How to programming for all characters in the game?

3
Why 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
4
Reuse
  • 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

6
Composition
  • ????????????? ????????????????????????????????????
    ??????
  • Example
  • public class Address
  • String number
  • String road
  • String city
  • String province
  • String postcode
  • public class Person
  • String name
  • String birthDate
  • Address address

7
Inheritance ?????????
  • ????????? (Inheritance) ????????????????????????
    OOP ??????????????????????????????????????????????
    ????
  • ???????????????????? ?????????????(attributes)????
    ?????????(behaviour) ???????????
  • ????????????????????????????????? ????????
    ??????? (parent class / superclass / based
    class)
  • ???????????????????????????? ????????
    ??????? (child class / subclass / derived class )

8
Inheritance class diagram
  • A ?????????????? B ??????? B ????????????????????
    A

A
???????
Vehicle
???????????????????????????
B
???????
Bicycle
Car
Helicopter
Racing
Sedan
9
Inheritance
  • ??????? ???????????????? ? ???? ? ?? (general)
  • ??????? ??????????????????? ??????????????????????
    ????????? (specialize)
  • ????????????????? ?????????????????????? Object
    ???????????????????
  • Attribute ??? method ??????????????????
    ????????????????????? ???????????????
    ????????????????????
  • ?????????????????? attribute ??? method ???????
  • ???????????????????????????????????????????????

10
Inheritance
  • Redesign the Student classes with inheritance

Student Id Name Major register() checkGrade()
Student
GradStudent thesis listConference()
UnderGradStudent checkActivity()
11
Keyword extends
  • ????????????????????????????????????????????
    ???????????????????????????????????? extends
  • Syntax
  • modifier class ??????????? extends
    ???????????
  • ????
  • public class GradStudent extends Student
  • public class Car extends Vehicle
  • public class Manager extends Employee

Employee
Manager
12
Ex. InherritanceDemo1
13
Ex. InherritanceDemo1
14
Keyword extends
Student
15
Keyword extends
  • ??????????????????????????????????????????????????
    ??
  • ??????????????????????????????????????????????????
    ????????

16
java.lang.Object
  • ?????????? ?????????????????????????? Object
    ?????????? package java.lang
  • ????????????????????????????????????????????????
    ??????????????????????? Object ?????????
  • ????????????? extends ????????????????????????????
    ?????????
  • ??????????????????????????????????????????????????
    ?

Object
Vehicle
Car
17
The methods of the Object class
  • Object ????????????????????? ??????????? Object
    ???????????????????
  • ??????????? Object ???????? Java API
    (http//java.sun.com) ????
  • public String toString() ?????? String ?????????
    object

18
Ex.
Object
Radio
ToStringDemo
19
Keyword null
  • ???????????????? object ????????? null ?????
    object ????????????????????????????????????? ????
    object ????????????????? Garbage Collector
  • Garbage Collector ?? JVM ?????????????? object
    ?????????????????????????? object
    ???????????????????

20
Method overriding
  • ??????????????????????????????????????????????????
    ???????????????? ???????? ????? method overriding
  • Overridden method ??? ?????????????????????????
    override
  • Overriding method ??? ??????????????????????
    override
  • ?????????????????? ????????? ????????????????????
    ??????????????????????? ??????????????????

21
Ex. Method overriding
22
Ex. Method overriding
23
Ex. Method overriding
24
Keyword this
  • this ????????? 2 ????
  • ??????? 1 ????????????????????????? ??? object
    ??? this ???????????? reference
    ?????????????????????????????????????

25
Keyword this
  • ??????? 2 ??? this ????? constructor ???? ?
    ??????

26
Keyword super
  • ??? super ??? 2 ????
  • ??????? 1 ??? super ?????????????????????????????

27
Keyword super
28
Keyword super
  • ??????? 2 ???????? constructor
    ?????????????????? ??????????????????????????????
    constructor ????????????

29
Find some errors
30
Shadowing Data Fields
  • ???? ?????????????????????????????????????????????
    ????
  • ???????????????????????????????????????????
    ??????????????????????????????????????????
  • ?????????????????????????????????????? ???????
    keyword super

31
Shadowing Data Fields
32
Shadowing Data Fields
33
Keyword final
  • Final class ??? class ??????????????? final
    ???????????????????????????

Compile error!!! InheritanceDemo1.java12 cannot
inherit from final Employee
34
Keyword final
  • Final method ?????????????? final
    ??????????????? override ??????

Compile error!!! InheritanceDemo1.java14
printData() in Manager cannot override
printData() in Employee overridden method is
final
35
Access modifier
  • Access modifier ???????????????????? ??????
    ???????? ?????????????????????????? ???????? 4
    ?????
  • public ???????????????????????????????????
  • protected ??????????????????????????????????
    package ????????????????????????????
  • package ????????????? package ???????????????????
    ?????????????????
  • private ????????????????????? private
    ?????????????????????????????????????????

36
Access modifier
  • ????????? access modifier
  • class
    attribute method
  • public / / /
  • protected X / /
  • package / / /
  • private X / /
  • ???????????????????? package ????????????????
    ?????? ?????????????????????? access modifier
    ???

37
Inheriting attributes and methods
  • ????????????????? ?????????????? ??????? private
    ???????????????????????????

38
Inheriting attributes and methods
39
Inheriting attributes and methods
  • ???????????????????? attributed ??? method
    ??????????????????????? protected ??? ???????
    package???
  • ?????????????????????? package ?????????????
    package ????????????????
  • package a
    package b

A
B
C
40
Inheriting attributes and methods
41
Inheritance and constructor
  • ???????? object ??????????????????? default
    constructor ????????? ?????????????????????????
    constructor ?????????????

42
Summary
  • ??? reuse class ????? 2 ?????? inheritance
    composition
  • ????????????????????????????????????????????????
  • ??????????????????? attribute ??? method ???????
    public, protected ???
  • ?????????? private ??????
  • Method overriding ????????????????????????????????
    ?????
  • this ??????????????????????????????????
  • super ?????????????????????????????????
  • Class ??????? final ??????????????????
  • Method ??????? final ????????? overrided ???
  • Constructor ???????????????????????????
Write a Comment
User Comments (0)
About PowerShow.com