42
8. Inheritance Faculty of Technology and Environment Prince of Songkla University, Phuket Campus 2/2551 976-140 Object-Oriented Programming 344-202 Introduction to OOP การโปรแกรมเชิงวัตถุ

8.Inheritance

Embed Size (px)

DESCRIPTION

สื่อการเรียนการสอน เรื่อง โปรแกรมเชิงวัตถุ โดยคุณ Kan http://learners.in.th/file/zniperlll

Citation preview

Page 1: 8.Inheritance

8. Inheritance

Faculty of Technology and Environment

Prince of Songkla University, Phuket Campus

2/2551

976-140 Object-Oriented Programming

344-202 Introduction to OOP

การโปรแกรมเชิงวัตถุ

Page 2: 8.Inheritance

2Object-Oriented Programming 2/2551

Why inheritance?

How to programming for all characters in the game?

Page 3: 8.Inheritance

3Object-Oriented Programming 2/2551

Why inheritance?

Student

Id

Name

Major

register()

checkGrade()

checkActivity()

Staff

Id

Name

Office

Salary

paySalary()

checkDayOff()

Lecturer

Id

Name

Office

Salary

assignCourse()

paySalary()

checkDayOff()

GradStudent

Id

Name

Major

thesis

register()

checkGrade()

listConference()

Reus

e

Page 4: 8.Inheritance

4Object-Oriented Programming 2/2551

Reuse

OOP เป็นภาษาที่สนับสนนุการน าคลาสกลบัไปใช้ใหม่ (reuse)

เช่น น าคลาสที่เราเคยสร้างไว้แล้วมาใช้ในโปรแกรมอื่นๆ ได้ หรือ

เอาคลาสเดิมมาใช้เป็นต้นแบบสร้างคลาสอื่นๆ ที่มีความสามารถเพิ่มขึ้น

Reuse ท าได้โดย

การสืบทอดคลาส (Inheritance)

การประกอบคลาส (Composition)

Page 5: 8.Inheritance

5Object-Oriented Programming 2/2551

"is a" versus "has a" Relationships

ความสัมพันธ์ระหว่างคลาส 2 แบบ ไดแ้ก่

ความสัมพันธ์แบบ is a : การสืบทอดคลาส (Inheritance)

• คลาสแรกเปน็ subclass ของคลาสที่สอง

a car is a vehicle

ความสัมพันธ์แบบ has a : การประกอบคลาส (Composition)

• คลาสแรกมีคลาสที่สองเป็น attribute

a car has wheels

Page 6: 8.Inheritance

6Object-Oriented Programming 2/2551

Composition

การประกอบคลาส เป็นการน าคลาสที่มอียู่มาประกอบเป็นคลาสใหม่

Example:

public class Address {

String number;

String road;

String city;

String province;

String postcode;

}

public class Person {String name;

String birthDate;

Address address;

}

Page 7: 8.Inheritance

7Object-Oriented Programming 2/2551

Inheritance การสืบทอด

การสืบทอด (Inheritance) เป็นคุณสมบัติที่ส าคญัของ OOP ที่ท าให้เรา

สามารถสร้างคลาสใหม่จากคลาสเดิมทีม่ีอยู่

คลาสใหม่ที่สร้างขึ้น ขยายคุณสมบัต(ิattributes)และความสามารถ

(behaviour) จากคลาสเดิม

คลาสเดิมที่ถูกน าคุณสมบตัิไปสืบทอด เรียกว่า

คลาสแม่ (parent class / superclass / based class)

คลาสใหม่ที่สืบทอดคุณสมบัติมา เรียกว่า

คลาสลูก (child class / subclass / derived class )

Page 8: 8.Inheritance

8Object-Oriented Programming 2/2551

Inheritance – class diagram

A เป็นคลาสแม่ของ B นั่นคือ B สืบทอดคุณสมบัตมิาจาก A

A

B

Vehicle

Racing Sedan

Bicycle CarHelicopter

คลาสแม่

คลาสลูก

สัญลักษณ์ลูกศรแสดงการสบืทอด

Page 9: 8.Inheritance

9Object-Oriented Programming 2/2551

Inheritance

คลาสแม ่จะมีความเป็นกลาง ๆ ทั่ว ๆ ไป (general)

คลาสลูก จะมีความเฉพาะเจาะจง มีลักษณะเฉพาะบางอย่างแตกต่างกัน

(specialize)

คลาสทุกคลาสในจาวา สืบทอดมาจากคลาสที่ชื่อ Object ซึ่งเป็นคลาส

มาตรฐาน

Attribute และ method ที่ประกาศในคลาสแม ่ถูกสืบทอดมายังคลาสลูก

และใช้งานได้เลย โดยไม่ต้องประกาศใหม่

คลาสลูกสามารถเพิ่ม attribute และ method ใหมไ่ด้

คลาสลูกสามารถแกไ้ขการท างานในเมธอดที่สืบทอดมาได้

Page 10: 8.Inheritance

10Object-Oriented Programming 2/2551

Inheritance

Redesign the Student classes with inheritance

Student

GradStudentUnderGradStudent

Student

Id

Name

Major

register()

checkGrade()

GradStudent

thesis

listConference()

UnderGradStudent

checkActivity()

Page 11: 8.Inheritance

11Object-Oriented Programming 2/2551

Keyword extends

การระบุว่าคลาสนั้นสืบทอดคุณสมบัติมาจากคลาสใด ท าไดใ้นการ

ประกาศคลาสโดยใชค้ีย์เวิร์ด extends

Syntax

modifier class ชื่อคลาสลูก extends ชื่อคลาสแม ่{ }

เช่น

public class GradStudent extends Student { }

public class Car extends Vehicle { }

public class Manager extends Employee { } Manager

Employee

Page 12: 8.Inheritance

12Object-Oriented Programming 2/2551

Ex. InherritanceDemo1

Page 13: 8.Inheritance

13Object-Oriented Programming 2/2551

Ex. InherritanceDemo1

Page 14: 8.Inheritance

14Object-Oriented Programming 2/2551

Keyword extends

Student

GradStudentUnderGradStudent

Page 15: 8.Inheritance

15Object-Oriented Programming 2/2551

Keyword extends

คลาสลูกสามารถใช้งานตัวแปรและเมธอดที่อยู่ในคลาสแม่ได้

แต่คลาสแม่ไม่สามารถใช้ตัวแปรหรือเมธอดทีป่ระกาศไว้ในคลาสลูก

Page 16: 8.Inheritance

16Object-Oriented Programming 2/2551

java.lang.Object

ในภาษาจาวา คลาสทุกคลาสสบืทอดมาจากคลาส Object ซึ่งอยู่ใน

package java.lang

เมื่อเราประกาศคลาสโดยไม่ระบุว่าจะสบืทอดจากคลาสใด คลาสน้ันจะ

สืบทอดจากคลาส Object อัตโนมัติ

แต่ถ้าระบุว่า extends จากคลาสใดก็จะหมายถึงสืบทอดจากคลาสน้ัน

คลาสในจาวาสืบทอดจากคลาสแมไ่ด้เพียงคลาสเดียวเท่านั้น

Car

Vehicle

Object

Page 17: 8.Inheritance

17Object-Oriented Programming 2/2551

The methods of the Object class

Object เป็นคลาสแม่ของทกุคลาส เมธอดในคลาส Object จึงใช้ในคลาส

ทุกคลาส

เมธอดในคลาส Object ดไูด้จาก Java API (http://java.sun.com) เช่น

public String toString() คืนค่า String ที่ใช้แทน object

Page 18: 8.Inheritance

18Object-Oriented Programming 2/2551

Ex.

Radio

Object

ToStringDemo

Page 19: 8.Inheritance

19Object-Oriented Programming 2/2551

Keyword null

การระบุให้ตัวแปร object มีค่าเป็น null ท าให้ object ที่เคยถูกอ้างถึงไม่

สามารถใช้งานได้อีก ซึ่ง object นั้นจะถูกท าลายโดย Garbage Collector

Garbage Collector ใน JVM ท าหน้าทีท่ าลาย object ที่ไม่ถูกอ้างถงึด้วยตัว

แปร object เพื่อคืนหน่วยความจ า

Page 20: 8.Inheritance

20Object-Oriented Programming 2/2551

Method overriding

การแกไ้ขหรือเพิ่มการท างานภายในเมธอดของคลาสลูกทีส่ืบทอดมาจาก

คลาสแม ่เรียกว่า การท า method overriding

Overridden method คือ เมธอดในคลาสแม่ที่ถูกท าการ override

Overriding method คือ เมธอดในคลาสลูกที่ท าการ override

เมธอดนั้นยังต้องมี ชื่อเมธอด ค่าที่ส่งคนืจากเมธอด ชนิดและจ านวน

พารามิเตอร์ เหมือนกับในคลาสแม่

Page 21: 8.Inheritance

21Object-Oriented Programming 2/2551

Ex. Method overriding

Page 22: 8.Inheritance

22Object-Oriented Programming 2/2551

Ex. Method overriding

Page 23: 8.Inheritance

23Object-Oriented Programming 2/2551

Ex. Method overriding

Page 24: 8.Inheritance

24Object-Oriented Programming 2/2551

Keyword this

this ใช้ได้กบั 2 กรณี

กรณีที่ 1 ใช้อ้างถึงตัวแปรภายในคลาส โดย object ใช้ this ในการอ้างถึง

reference ของตัวเองเมื่อเรียกใช้ตวัแปรหรือเมธอด

Page 25: 8.Inheritance

25Object-Oriented Programming 2/2551

Keyword this

กรณีที่ 2 ใช้ this เรียก constructor อื่น ๆ ในคลาส

Page 26: 8.Inheritance

26Object-Oriented Programming 2/2551

Keyword super

ใช้ super ได้ 2 กรณี

กรณีที่ 1 ใช้ super เรียกตัวแปรหรือเมธอดในคลาสแม่

Page 27: 8.Inheritance

27Object-Oriented Programming 2/2551

Keyword super

Page 28: 8.Inheritance

28Object-Oriented Programming 2/2551

Keyword super

กรณีที่ 2 ใช้เรียก constructor ของคลาสแมใ่หท้ างาน แต่ต้อง

เรียกที่บรรทัดแรกสุดของ constructor นั้นเท่าน้ัน

Page 29: 8.Inheritance

29Object-Oriented Programming 2/2551

Find some errors…

Page 30: 8.Inheritance

30Object-Oriented Programming 2/2551

Shadowing Data Fields

ปกต ิคลาสลกูสามารถใช้งานตัวแปรที่ประกาศในคลาสแมไ่ด้เลย

แต่ถ้าประกาศตัวแปรในคลาสลูกเหมือนกับคลาสแม ่การอ้างถึงตัวแปร

นั้นจะเป็นตัวแปรของคลาสลูก

หากต้องการใช้งานตัวแปรที่อยู่ในคลาสแม่ ต้องใช ้keyword super

Page 31: 8.Inheritance

31Object-Oriented Programming 2/2551

Shadowing Data Fields

Page 32: 8.Inheritance

32Object-Oriented Programming 2/2551

Shadowing Data Fields

Page 33: 8.Inheritance

33Object-Oriented Programming 2/2551

Keyword final

Final class : ถ้า class ใดประกาศให้เป็น final จะไม่สามารถน ามาสืบทอด

ได้อีก

Compile error!!!

InheritanceDemo1.java:12:

cannot inherit from final

Employee

Page 34: 8.Inheritance

34Object-Oriented Programming 2/2551

Keyword final

Final method: ถ้าเมธอดใดเป็น final จะไม่สามารถน ามา override ได้อีก

Compile error!!!InheritanceDemo1.java:14:

printData() in Manager cannot override printData() in Employee;

overridden method is final

Page 35: 8.Inheritance

35Object-Oriented Programming 2/2551

Access modifier

Access modifier ใช้ก าหนดหน้าชื่อคลาส ตัวแปร และเมธอด เพื่อบอก

ระดับการเข้าใช้งาน แบ่งเป็น 4 ระดับ

public คลาสอื่นทุกคลาสสามารถเรียกใช้งานได้

protected คลาสที่เป็นคลาสลูกและคลาสที่อยู่ใน package เดียวกันสามารถ

เรียกใช้งานได้

package คลาสที่อยู่ใน package เดียวกันเท่านั้นสามารถเรียกใช้งานได้

private ตัวแปรและเมธอดที่เป็น private จะถูกเรียกใช้งานได้เฉพาะภายใน

คลาสเท่านั้น

Page 36: 8.Inheritance

36Object-Oriented Programming 2/2551

Access modifier

การใช้งาน access modifier

class attribute method

public / / /

protected X / /

package / / /

private X / /

การก าหนดให้เป็นระดับ package คือการประกาศคลาส ตวัแปร

หรือเมธอดที่ไม่ได้ระบุ access modifier ไว้

Page 37: 8.Inheritance

37Object-Oriented Programming 2/2551

Inheriting attributes and methods

การสืบทอดคลาสนั้น ตัวแปรและเมธอด ที่เป็น private จะไม่ถูกสืบทอด

ใหแ้กค่ลาสลูก

Page 38: 8.Inheritance

38Object-Oriented Programming 2/2551

Inheriting attributes and methods

Page 39: 8.Inheritance

39Object-Oriented Programming 2/2551

Inheriting attributes and methods

คลาสลูกสามารถเข้าถึง attributed และ method ของคลาสแม่ที่ประกาศ

เป็น protected ได้ แม้ต่าง packageกัน

ตัวแปรหรือเมธอดที่เป็น package จะใช้ได้เฉพาะ package เดียวกันเท่านั้น

package a package b

B

A

C

Page 40: 8.Inheritance

40Object-Oriented Programming 2/2551

Inheriting attributes and methods

Page 41: 8.Inheritance

41Object-Oriented Programming 2/2551

Inheritance and constructor

การสร้าง object ของคลาสลูกจะไปเรียก default constructor ในคลาส

แม่ แต่คลาสลูกไมส่ามารถสืบทอด constructor จากคลาสแม่ได้

Page 42: 8.Inheritance

42Object-Oriented Programming 2/2551

Summary

การ reuse class ท าได้ 2 แบบคือ inheritance & composition

คลาสลูกสืบทอดได้จากคลาสแม่เพียงคลาสเดียวเท่านั้น

คลาสลูกเข้าถึงเฉพาะ attribute และ method ที่เป็น public, protected ได้

แต่เข้าถึง private ไม่ได้

Method overriding คือการแก้ไขเมธอดที่สืบทอดมาจากคลาสแม่

this ใช้อ้างถึงตัวแปรและเมธอดในคลาสนั้น

super ใช้อ้างถึงตัวแปรและเมธอดในคลาสแม่

Class ที่เป็น final ไม่สามารถสืบทอดได้

Method ที่เป็น final ไม่สามารถ overrided ได้

Constructor ไม่สามารถสืบทอดมายังคลาสลูก