18
Inheritance and Method Overriding อาจารย์สมเกียรติ ช่อเหมือน สาขาวิชาวิศวกรรมซอฟต์แวร์ คณะวิทยาศาสตร์และเทคโนโลยี ([email protected])

Inheritance and Method Overriding

Embed Size (px)

Citation preview

Page 1: Inheritance and Method Overriding

Inheritance and Method Overriding

อาจารย์สมเกียรติ ช่อเหมือนสาขาวิชาวิศวกรรมซอฟต์แวร์ คณะวิทยาศาสตร์และเทคโนโลยี

([email protected])

Page 2: Inheritance and Method Overriding

Inheritance and Method Overriding

• แนวคิดที่ส ำคัญของกำรสืบทอด (Inheritance)– เข้ำใจวิธีกำรใช้คลำสนำมธรรมเพื่อสะท้อนคุณลักษณะทั่วไป

– ใช้ UML แสดงกำรสืบทอด

• กำรท ำ method Overriding– กำรแทนที่เมธอด

• ควำมเหมำะสมในกำรใช้ “Super”– Implement ในภำษำ Java

Page 3: Inheritance and Method Overriding

Object Oriented Programming using Java

1) Object Families2) Generalisation and Specialisation3) Inheritance4) Implementing Inheritance in Java5) Constructors6) Constructor Rules7) Access Control8) Abstract Classes9) Overriding Methods10) The ‘Object’ Class11) Overriding toString() defined in ‘Object’

Page 4: Inheritance and Method Overriding

Object Families

• สิ่งมีชีวิตบนโลกมีควำมเกี่ยวข้องกัน แบบครอบครัว แนวคิดกำรส่งต่อ มรดก ไปยังลูกหลำนของ พ่อแม่ เป็นแนวคิดที่ส ำคัญของหลักกำร OOP

• ปกติกำรใช้ constructors and access control (public/private)

• ปัญหำเฉพำะที่เกี่ยวข้องกับกำรสืบทอด คือ กำรพิจำรณำคลำสแบบนำมธรรมและเมธอดที่ส ำคัญในกำรสืบทอด

Page 5: Inheritance and Method Overriding

Generalisation and Specialisation

• Class เป็นรูปแบบในกำรพิจำรณำรำยละเอียดและสร้ำงวัตถุที่แตกต่ำงกัน– เช่นนักศึกษำรหัส 594602001 เป็นตัวอย่ำงของนักศึกษำ

– รหัส 594602001 คือนักศึกษำ

• Class สำมำรถหำควำมสัมพันธ์ทีค่ล้ำยกัน เพื่อจัดกลุ่มและล ำดับชั้น

• ตัวอย่ำงเช่น อำณำจักรสัตว์ทำงชีววิทยำ

กำรจ ำแนกสิ่งของตำมเกณฑ์

Page 6: Inheritance and Method Overriding

Inheritance

• กำรระบุลักษณะทั่วไปไว้ด้ำนบนของล ำดับชั้นและเฉพำะเจำะจงมำกขึ้นในล ำดับถัดไป ตำมหลักกำรเชิงวัตถุ (generalization and

specialization)

• ลักษณะด้ำนบนจะถูกถ่ำยทอดให้ Class หรือ Object ในล ำดับถัดไปแบบอัตโนมัติ เรียกว่ำ “inheritance”

• ตัวอย่ำงเช่น เมื่อพิจำรณำสิ่งพิมพ์ทั้งสองประเภท คือ หนังสือและนิตยสำร

– UML Class

Page 7: Inheritance and Method Overriding

Inheritance

• วิเครำะห์สิ่งที่เหมือนและต่ำงกัน สำมำรถรวมกันได้เป็น

• สำมำรถสืบทอดเป็นBook และ Magazine

Page 8: Inheritance and Method Overriding

Inheritance

Page 9: Inheritance and Method Overriding

Implementing Inheritance in Java

• Superclass เป็นคลำสที่มีคุณสมบัติพื้นฐำน

• Subclass สำมำรถระบุกำรสืบทอดคุณสมบัติจำก superclass – โดยใช้ค ำหลัก extends

• ตัวอย่ำงเช่น

class MySubclass extends MySuperclass{// additional instance variables and// additional methods}

Page 10: Inheritance and Method Overriding

Constructors

• แตล่ะClass (ไม่ว่ำจะSub หรือ Super) – ควรมีกำรห่อหุ้ม

– มีกำรก ำหนดเริ่มต้นของตัวแปร กำรตั้งค่ำที่เกี่ยวข้องกับสถำนะ

• Constructors ใช้ส ำหรับก ำหนดค่ำเริ่มต้นทั่วไปของ Class

• Subclass สำมำรถมีคอนสตรัคเตอรข์องตัวเองส ำหรับก ำหนดค่ำเริ่มต้น

• แต่หำกต้องกำรใช้ค่ำเริ่มต้นของ Superclass ต้องใช้ค ำหลัก “super”Class MySubClass extends MySuperClass{public MySubClass (sub-parameters){super(super-parameters);// other initialization}

Page 11: Inheritance and Method Overriding

Constructors

• ในกำรสร้ำง Class โดยกำรเรียกใช้ Constructors ถือเป็นค ำสั่งแรก

• พำรำมิเตอร์ที่ส่งผ่ำนไปยังSubclass public Publication (String pTitle, double pPrice, int pCopies){title = pTitle;// etc.}

public Book (String pTitle, String pAuthor, double pPrice,int pCopies){super(pTitle, pPrice, pCopies);author = pAuthor;//etc.}

Page 12: Inheritance and Method Overriding

Constructor Rules

• หำก superclass มี parameterless (หรือเร่ิมต้น) คอนสตรัคเตอรน์ี้จะถูกเรียกโดยอัตโนมัติ

• หำกไม่มีกำรเรียกใช้อย่ำงชัดเจนกับซูเปอร์จะต้องสร้ำง constructor ของ subclass

Page 13: Inheritance and Method Overriding

Access Control

• กำรใช้ protected เพื่อให้เข้ำถึงได้จำก Subclass

• กำรสร้ำงเมธอดในกำรเข้ำถึงและปรับเปลี่ยนค่ำprivate int copies;public int getCopies (){return copies;}public void setCopies(int pCopies){copies = pCopies;}

วิธีกำรเหล่ำนี้ช่วยให้ superclass ควบคุมกำรเข้ำถึงตัวแปรแบบ Private

// in Bookpublic void orderCopies(int pCopies){setCopies(getCopies() + pCopies);}

// and in Magazinepublic void recvNewIssue(String pNewIssue){setCopies(orderQty);currIssue = pNewIssue;}

Page 14: Inheritance and Method Overriding

Abstract Classes

• แนวคิดในกำรสร้ำงคลำสที่ร่ำงขึ้น และป้องกันกำรสร้ำงวัตถุจำกคลำสประเภทนี้

• กำรรวบรวมคุณลักษณะทั่วไป ส ำหรับสืบทอดไปเป็น Subclass

• โดยประกำศ “abstract”

abstract class SuperClassName{// etc.}

Page 15: Inheritance and Method Overriding

Overriding Methods

• Subclass สำมำรถสืบทอดเมธอดของ Superclass ได้

• แต่สำมำรถเปลี่ยนแปลงกำรกำรท ำงำนของเมธอดไปจำก Superclass

• เรำเรียกว่ำ “overriding method”

• ซึ่งเป็นคุณสมบัติหนึ่งของกำรสืบทอด (inherited)

• สำมำรถปรับปรุงเมธอดและท ำให้เมธอดเหมำะสมกับ

Subclass ยิ่งขึ้น public class DiscMag extends Magazine{ // the constructorpublic DiscMag (String pTitle, double pPrice, int pOrderQt, String pCurrIssue, int pCopies){super(pTitle, pPrice, pOrderQty, pCurrIssue, pCopies);}// the overridden methodpublic void recvNewIssue(String pNewIssue){super.recvNewIssue(pNewIssue);System.out.println("Check discs attached to thismagazine");}}

Page 16: Inheritance and Method Overriding

The ‘Object’ Class

• Object หรือวัตถุ เป็นเสมือนตัวแทนของทุกอย่ำงในคลำสที่ก ำหนดขึ้น

• โดยเป็นส่วนบนสุดในกำรเข้ำถึงสิ่งต่ำง ๆ ที่อยู่ภำยใน

• ประกำศ เพื่อใช้งำน object จำก Class

• “new” เพื่อก ำหนดค่ำเริ่มต้นจำก Class ให้กับ object

Page 17: Inheritance and Method Overriding

Overriding toString() defined in ‘Object’

Page 18: Inheritance and Method Overriding

Summary

• Inheritance ช่วยให้สะท้อนคุณสมบัติทั่วไปและพฤติกรรม โดยจ ำลองไว้ใน superclass

• subclass ถูกน ำมำใช้ในกำรจ ำลองคุณลักษณะเฉพำะและพฤติกรรม• code ที่เขียนใน superclass สืบทอดไปยัง subclasses ทั้งหมด ถ้ำต้องกำร

แก้ไขหรือปรับปรุง code สำมำรถปรับที่ superclass จะส่งผลกระทบต่อ subclasses ทั้งหมด ช่วยลดกำรเขียนCode ในโปรแกรม

• constructors ที่สำมำรถน ำไปใช้กับ Subclass • superclass สำมำรถก ำหนดเป็น Abstract เพื่อป้องกันกำรสร้ำงวัตถุ• สำมำรถท ำ override เพื่อปรับปรุงเมธอดให้สอดคล้องกับ Subclass• ใน Java ทุกคลำสสืบทอดมำจำก Class “Object”• Object เป็นกำรก ำหนดกำรด ำเนินกำรแบบสำกล เป็นตัวแทนในกำรใช้

ประโยชน์จำกคลำสที่ก ำหนด