Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร....
Transcript of Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร....
![Page 1: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/1.jpg)
Lec09 Inheritance
อ.ดร. นฐพงศ สงเนยมWebsite www.siam2dev.com
Email : [email protected]
Last update : 23-01-2561
23/01/61 1อ.ดร. นฐพงศ สงเนยม
![Page 2: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/2.jpg)
Inheritance
2อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 3: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/3.jpg)
Agenda• What is an inheritance ?• การสบทอด• การเขาใชแบบ protected• พอลมอรฟซม• การสบทอดในภาษาจาวา• คลาส Object
23/01/61 3อ.ดร. นฐพงศ สงเนยม
![Page 4: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/4.jpg)
สตวตางๆ
• คลาสแมว– แอตทรบวต ไดแก อาย และ ความหว– เมธอด ไดแก กน() และ นอน()
• คลาสปลา– แอตทรบวต ไดแก อาย ความหว และชอ (เชน ปลานโม)– เมธอด ไดแก กน() และ นอน()
• คลาสลง– แอตทรบวต ไดแก อาย และ ความหว– เมธอดไดแก กน() นอน() และเกบลกมะพราว()
Problem Domain ?จากขอความน ยงไมไดใชเรอง
Problem Domain
23/01/61 4อ.ดร. นฐพงศ สงเนยม
![Page 5: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/5.jpg)
แอททรบวตและเมธอดทคลายกน
23/01/61 5อ.ดร. นฐพงศ สงเนยม
![Page 6: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/6.jpg)
การสบทอด (INHERITANCE)
Inheritance เปนรปแบบของการนากลบมาใชใหมของซอฟตแวร ซงคลาสใหมจะถกสรางจากการรบสงตางๆ จากคลาสทมอย ทงสวน Attribute และสวนของ Method และคลาสใหมยงสามารถเพมเตมความสามารถบางประการตามตองการ
ดงนนคลาสทถกสรางใหมจะไดรบคณสมบตของคลาสเดมและเพมคณสมบตบางประการ
• แกปญหาการเขยนโปรแกรมซาซอนกนได • เมธอดหรอแอตทรบวตทคลาสตางๆ มรวมกนจะถกนาไปใสในคลาสแม • คลาสลกจะสบทอดเมธอดและแอตทรบวตคลาสแมโดยอตโนมต23/01/61 6อ.ดร. นฐพงศ สงเนยม
![Page 7: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/7.jpg)
Class Diagram w ith Multiple Classes
account
*
BankAccount
- balance : double
+ BankAccount(initBalance:double)+ getBalance : double+ deposit(amt : double)+ withdraw(amt : double)
Customer
- firstName : String- lastName : String- account : BankAccount []
+ Customer(f:String, l:String)+ getFirstName : String+ getLastName : String+ setAccount( acct:BankAccount)+ getAccount( ) : BankAccount
7อ.ดร. นฐพงศ สงเนยม
Personal Corporate
SavingCurrent
23/01/61
![Page 8: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/8.jpg)
จากรปกอนหนาน
สามารถอธบายไดวา ลกคาแบงออกเปน 2 ประเภท คอลกคาสวนบคล และลกคากลมบรษท และลกคาธนาคารมบญชธนาคารอยางนอย(*) 0 คอไมมเลย หรอ มากทสด กบญชกได(ในความเปนจรงขนอยกบ เงอนไขทางธรกจ ของแตละธนาคาร)โดยทบญชธนาคารนนแบงออกเปน 2 ประเภท คอบญชออมทรพย และ กระแสรายวน (จากรป)
23/01/61 8อ.ดร. นฐพงศ สงเนยม
![Page 9: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/9.jpg)
Aggregation Relationship
นศ. อาจสงสย วา สญลกษณในรป คออะไร ?- เปนสญลกษณของ aggregataion abstraction ซงเปนความสมพนธแบบ Has a หมายถง ลกคามบญชธนาคาร
23/01/61 9อ.ดร. นฐพงศ สงเนยม
![Page 10: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/10.jpg)
Class Diagram w ith Multiple Classes
account
*
BankAccount
- balance : double
+ BankAccount(initBalance:double)+ getBalance : double+ deposit(amt : double)+ withdraw(amt : double)
Customer
- firstName : String- lastName : String- account : BankAccount []
+ Customer(f:String, l:String)+ getFirstName : String+ getLastName : String+ setAccount( acct:BankAccount)+ getAccount( ) : BankAccount
10อ.ดร. นฐพงศ สงเนยม
Personal Corporate
SavingCurrentIs A
Is A
23/01/61
![Page 11: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/11.jpg)
ถาจะเตม multiplicity ทาอยางไร
account
*
BankAccount
- balance : double
+ BankAccount(initBalance:double)+ getBalance : double+ deposit(amt : double)+ withdraw(amt : double)
Customer
- firstName : String- lastName : String- account : BankAccount []
+ Customer(f:String, l:String)+ getFirstName : String+ getLastName : String+ setAccount( acct:BankAccount)+ getAccount( ) : BankAccount
Personal Corporate
SavingCurrent
1 *
1 1
23/01/61 11อ.ดร. นฐพงศ สงเนยม
![Page 12: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/12.jpg)
บญชเงนฝากธนาคาร
23/01/61 12อ.ดร. นฐพงศ สงเนยม
![Page 13: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/13.jpg)
การทา generalization ไมตองใส Multiplicity
• ในการทา generalization abstraction จะไมมการใส Multiplicity ลงไปดงตวอยาง
1
*
23/01/61 13อ.ดร. นฐพงศ สงเนยม
![Page 14: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/14.jpg)
การใส multiplicity
จะใสเฉพาะความสมพนธทเปนแบบ• Associations Relationship• Aggregations Relationship
23/01/61 14อ.ดร. นฐพงศ สงเนยม
![Page 15: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/15.jpg)
การทา generalization ไมตองใส Multiplicity
Implementation class diagram รปน
ตองทาอยางไร
23/01/61 15อ.ดร. นฐพงศ สงเนยม
![Page 16: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/16.jpg)
สญลกษณน ไมใชสญลกษณมาตรฐานของ UMLแตเปนของซอฟตแวร (case tools)
23/01/61 16อ.ดร. นฐพงศ สงเนยม
![Page 17: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/17.jpg)
• จากรปนสามารถอธบาย
ไดวา บคคลแบงออกเปน
พนกงาน และ ลกคา หรอ
อกนยหนงคอทง พนกงาน
และ ลกคากมสถานะของ
คนทวไปเหมอนกน
23/01/61 17อ.ดร. นฐพงศ สงเนยม
![Page 18: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/18.jpg)
1..N
23/01/61 18อ.ดร. นฐพงศ สงเนยม
![Page 19: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/19.jpg)
ลาดบท รายการสนคา ราคา จานวน จานวนเงน
1 com 35000 5 1750002. Printer 2000 10 200003. Speaker 1500 10 150004. Adobe Dream 100000 5 500000
…. … ……
LineItem
1..n
* คอ 0..n
ใบสงซอสนคา / Order
23/01/61 19อ.ดร. นฐพงศ สงเนยม
![Page 20: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/20.jpg)
23/01/61 20อ.ดร. นฐพงศ สงเนยม
![Page 21: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/21.jpg)
23/01/61 21อ.ดร. นฐพงศ สงเนยม
![Page 22: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/22.jpg)
Inheritance การรบถายทอดคณสมบต(Inheritance)
หมายถง การทคลาสหนงๆ รบถายคณสมบต (inherit) ทงคาคณลกษณะ (Attribute) และพฤตกรรม (Method) มาจากอกคลาสหนง
แตไมรวมถง คอนสตรคเตอร(Constructor)
Employee
+ name : String+ salary : double+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
22อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 23: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/23.jpg)
Inheritance
Class Diagram of “BankAccount” class and “SavingsAccount” class
BankAccount
+BankAccount(intibalance:double)+getBalance( ) : double+deposit(amount : double)+withdraw(amount : double)
- balance : double
SavingsAccount
- interestRate : double
+SavingsAccount(intibalance:double, rate:double)
รบถายทอดคณสมบต(inherit)
23อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 24: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/24.jpg)
Sub classing พจารณาคลาส Employee
เราสามารถสรางวตถทมคลาสเปนพนกงาน ไดโดยการกาหนดคลาส“Employee”
ถาเราตองการสรางคลาสของ “Manager” ทยงคงคณสมบตเปน“Employee” คนหนง แตมคณสมบตบางอยางเพมเตม
public class Employee {public String name;public double salary;public MyDate birthDate;
public String getDetails( ) { … }}
Employee
+ name : String+ salary : double+ birthDate : MyDate
+ getDetails( ) : String
24อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 25: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/25.jpg)
พจารณาคลาส Manager
โครงสรางคลาส “Manager” กบ “Employee” ซาซอนกน เราใชวธการเชงวตถสรางคลาสใหม จากคลาสเดมทมอยแลว วธการนเราเรยกวา “Subclassing”
public class Manager {public String name;public double salary;public MyDate birthDate;public String department;
public String getDetails( ) { … }}
Manager
+ name : String+ salary : double+ birthDate : MyDate+ department : String
+ getDetails( ) : String
Subclassing
25อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 26: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/26.jpg)
UML DiagramSubclassing
Employee
+ name : String+ salary : double+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
การสรางคลาสใหมจากคลาสเดมทมอยแลว สนบสนนแนวคดของการนา Source Code กลบมาใชใหม
Superclass
Subclass(Child Class)
(Parent Class)
26อ.ดร. นฐพงศ สงเนยม
Based class
derive class this super
23/01/61
![Page 27: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/27.jpg)
การใช This References
เราจะทราบถงสงทอางองเมอไดทาการสรางวตถแลว แตถาการเขยนสงทอยภายในคลาส ยงไมไดถกสรางเปนวตถ เราจะอางไดอยางไร คาวา “this” เปนวธทชวยใหการอางองตวมนเองโดยยงไมตองสรางวตถกสามารถทาได
• Using this with a Field• Using this with a Constructor
23/01/61 อ.ดร. นฐพงศ สงเนยม 27
![Page 28: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/28.jpg)
public class Point {
public int x = 0;
public int y = 0;
//constructor public Point(int a, int b){
x = a; y = b;
}}
Using this with a Field
public class Point { public int x = 0; public int y = 0; //constructor public Point(int x, int y){
this.x = x; this.y = y;
}}
Point
+ x : integer+ y : integer
+ Point(a:integer,b:integer)
![Page 29: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/29.jpg)
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 0, 0);}
public Rectangle(int width, int height) {
this(0, 0, width, height);}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
... }
Using this with a Constructor
![Page 30: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/30.jpg)
Inheritance การรบถายทอดคณสมบต (Inheritance)
หมายถง การทคลาสหนงๆ รบถายคณสมบต (inherit) ทงคาคณลกษณะ(Attribute) และพฤตกรรม (Method) มาจากอกคลาสหนง
แตไมรวมถง คอนสตรคเตอร(Cosntructor)
*** คอการท class ทตางกนม Attributes และ Methods ทเหมอนกน
Employee
+ name : String+ salary : double+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
30อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 31: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/31.jpg)
Single Inheritance Single Inheritance
หมายถง การทคลาสหนงๆ รบถายทอดคณสมบต (inherit) ทง Attribute และMethod มาจากอกคลาสหนง เราเรยกวาคลาส “Manager” รบถายทอดคณสมบต (inherit) มาจาก คลาส“Employee”
Employee
+ name : String+ salary : double+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
31อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 32: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/32.jpg)
Multiple Inheritance Multiple Inheritance
หมายถง การทคลาสหนงๆ รบถายทอดคณสมบต (inherit) ทงคาคณลกษณะ และพฤตกรรมมาจากคลาสมากกวา 1 คลาส
People
+ Name : String+ id : String+ birthDate : MyDate
+ getDetails( ) : String
Employee
+ company : String+ salary : double+ startDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
ภาษาจาวา ไมสนบสนนการทาMultiple Inheritance แตสามารถรบสบทอดคณสมบตจากหลายคลาสไดโดยใช “Interface”
32อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 33: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/33.jpg)
UML Diagram
Java Inheritance
Employee
+ name : String+ salary : double+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
public class Manager extends Employee {
public String department; }
จาวาpublic class Employee {
public String name;public double salary;public MyDate birthDate;
public String getDetails( ) { … }}
33อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 34: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/34.jpg)
การกาหนดคลาสทรบคณสมบตสบทอดในภาษาจาวา สามารถทาไดดงน
<modifier> class child-class extends parent-class {
:}
ตวอยาง
Defining Extending Class in Java
public class Manager extends Employee {public String department;
}
34อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 35: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/35.jpg)
“super” and “this” keyword “this” keyword
ใชอางองถงคลาสปจจบน สามารถใชอางองถง Attribute หรอ Method ของคลาสปจจบน ไดโดยใส . แลวตามดวยชอ Attribute หรอ ชอMethod
“super” keyword ใชอางองถง superclass ของคลาสปจจบน
super( ) อางองถง constructor ของ superclass super.method( ) อางองถง method ของsuperclass
35อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 36: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/36.jpg)
Top-Middle-Bottom (1)class Top {
public Top( ) { System.out.println("Top()"); }}
class Middle extends Top {public Middle( ) { System.out.println("Middle()"); }
}
class Bottom extends Middle {public Bottom( ) { System.out.println("Bottom()"); }
}
public class Test_inheritance01 {public static void main(String[] args) {
new Bottom();}
}36อ.ดร. นฐพงศ สงเนยม
Test_inheritance01
Chaining constructor23/01/61
![Page 37: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/37.jpg)
สราง Project : Test_inheritance01
23/01/61 37อ.ดร. นฐพงศ สงเนยม
![Page 38: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/38.jpg)
23/01/61 38อ.ดร. นฐพงศ สงเนยม
![Page 39: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/39.jpg)
ผลการรน
Chaining constructor
23/01/61 39อ.ดร. นฐพงศ สงเนยม
![Page 40: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/40.jpg)
Top-Middle-Bottom (2)class Top {
public Top( ) { System.out.println("Top()"); }}
class Middle extends Top {public Middle( ) { super( ) ; System.out.println("Middle()"); }
}
class Bottom extends Middle {public Bottom( ) { super( ); System.out.println("Bottom()"); }
}
public class Tester2 {public static void main(String[] args) {
new Bottom();}
}40อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 41: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/41.jpg)
Overriding Methods subclass/child class สามารถเปลยนแปลงพฤตกรรม ทรบถายทอด(inherit) มาจาก superclass/parent classได subclass สามารถกาหนด Method ทมหนาท/พฤตกรรม ตางจาก Method ของsuperclass ไดแตตองมส งตอไปนทเหมอนกน
Method Name Return Type Argument list
Employee
+ name : String+ salary : double+ birthDay : MyDate
+ getDetails( ) : String
Manager
- department : String
+ getDetails( ) : String
41อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 42: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/42.jpg)
Polymorphism
• Overloading – การยอมใหชอเมธอดซากนได
• Overriding
23/01/61 42อ.ดร. นฐพงศ สงเนยม
![Page 43: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/43.jpg)
Overriding Method (1)class Employee {protected String name;protected double salary;protected String birthDate;
public Employee(String n, double s, String bd) {name = n;salary = s;birthDate = bd;
}
public String getDetails( ) {return “name: ” + name + “, salary: ” + salary
+ “ , bd: ” + birthDate + “ /n”;}
} 43อ.ดร. นฐพงศ สงเนยม
สราง Project : Test_inheritance02
อาจทาเปนเมธอด toString()เพอเปนการอธบายวา วตถ พนกงาน
คออะไร จะตองบอก ชอ บอกตาแหนง บอกเงนเดอน พนกงาน
23/01/61
![Page 44: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/44.jpg)
Employee
23/01/61 44อ.ดร. นฐพงศ สงเนยม
![Page 45: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/45.jpg)
สราง Project : Test_inheritance02
23/01/61 45อ.ดร. นฐพงศ สงเนยม
![Page 46: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/46.jpg)
สรางคลาส Employee ไมตองใส public หนาคลาส
• สรางในไฟลเดยวกนกบ Test_inheritance2
23/01/61 46อ.ดร. นฐพงศ สงเนยม
![Page 47: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/47.jpg)
Manager
23/01/61 47อ.ดร. นฐพงศ สงเนยม
![Page 48: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/48.jpg)
ปรบปรง Manager ใหม
Overriding Method
23/01/61 48อ.ดร. นฐพงศ สงเนยม
![Page 49: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/49.jpg)
Overriding Methods (2)class Manager extends Employee {
public String department;
public Manager(String n, double s, String bd, String dept) {name = n;salary = s;birthDate = bd;department = dept;
}
public String getDetails( ) {return "name:"+ name + ",salary:"+salary
+"bd:“ + birthDate +", department:"+ this.department;
}}
49อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 50: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/50.jpg)
Super & Overriding Methods public class Manager extends Employee {
public String department;
public Manager(String n, double s, MyDate bd, String dept) {name = n;salary = s;birthDate = bd;department = dept;
}
public String getDetails( ) {return super.getDetails()
+", department:"+this.department;}
}
50อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 51: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/51.jpg)
Super & Overriding Methods
public class Manager extends Employee {public String department;
public Manager(String n, double s, MyDate bd, String dept) {super(n, s, bd);department = dept;
}
public String getDetails( ) {return super.getDetails()
+", department:"+this.department;}
}
51อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 52: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/52.jpg)
TestCompanypublic class TestCompany {
public static void main(String[] args) {Employee jeff = new Employee( "Jeffrey"
, 3000.0, new MyDate(2, 3, 1970) );
Manager rob = new Manager(" Robert", 5000.0, new MyDate(3, 4, 1965)
, ”Sales");System.out.println(jeff.getDetails( ));System.out.println(rob.getDetails( ));
}}
“TestCompany.java”
52อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 53: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/53.jpg)
Test
23/01/61 53อ.ดร. นฐพงศ สงเนยม
![Page 54: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/54.jpg)
Protected # Employee
# name : String# salary :double# birthdate : String
Manager
23/01/61 54อ.ดร. นฐพงศ สงเนยม
![Page 55: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/55.jpg)
Employee# name : String# salary :double# birthdate : String
Manager
CustomerTake care / Serve
1 1
+addCustomer(C:Customer)+serveCustomer(C:Customer)
public void addCustomer(Customer c) {….
}
23/01/61 55อ.ดร. นฐพงศ สงเนยม
![Page 56: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/56.jpg)
Employee# name : String# salary :double# birthdate : String# e-mail : String
Manager
CustomerTake care / Serve
1 1
+addCustomer(C:Customer)+serveCustomer(C:Customer)
public void addCustomer(Customer c) {….
}
CEO
23/01/61 56อ.ดร. นฐพงศ สงเนยม
![Page 57: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/57.jpg)
Employee# name : String# salary :double# birthdate : String# e-mail : String
Manager
CustomerTake care / Serve
1 1 +addCustomer(C:Customer)+serveCustomer(C:Customer)
public void addCustomer(Customer c) {….
}
CEO
CompanyWork for
1 *+ CompanyName : String+ Address : String
23/01/61 57อ.ดร. นฐพงศ สงเนยม
![Page 58: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/58.jpg)
23/01/61 58อ.ดร. นฐพงศ สงเนยม
![Page 59: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/59.jpg)
เพม getName() และ setName()
Protected Attributes
23/01/61 59อ.ดร. นฐพงศ สงเนยม
![Page 60: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/60.jpg)
• ลกคาจะไมไดรบการถายทอด Attributes มา เนองจากลกคาไมใชพนกงาน
23/01/61 60อ.ดร. นฐพงศ สงเนยม
![Page 61: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/61.jpg)
TestCompany
main
Employee & Manger Example
Employeename=“Jeffrey”
jeff
Salary=1000.0
birthday=jeffdate
getDetails
MyDate
toStringsetDate
jeffdate
day=2;month=3;
year=1970;
MyDate
toStringsetDate
robdate
day=3;month=4;
year=1965;
Managername=“Robert”
rob
Salary=5000.0
birthday=Robdate
getDetails
department=“Sales”
61อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 62: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/62.jpg)
Access Control การควบคมการเขาถงขอมล (Access Control)
คอ วธการเชงวตถ ทภาษาจาวามไวเพอ ใชเขยนโปรแกรมสนบสนนคณสมบต Encapsulation และ Information Hiding การระบคา Access Control Specifier เปนการควบคมการเขาถงขอมล โดยจะใชระบกอนหนา ตวแปร คาคงท เมทธอด และคลาส คา Access Control Specifier ไดแก
public - เปดใหเขาถงขอมลไดจากทกๆท private - เปดใหเขาถงขอมลไดภายในขอบเขตทกาหนด protected - เปดใหเขาถงขอมลไดจากการสบทอดคณสมบต
62อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 63: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/63.jpg)
class BankAccount {private double balance;
public BankAccount(double initAmount) {balance = initAmount;
}
public void deposit(double amount) {balance = balance + amount;
}
public void withdrawn(double amount) {balance = balance - amount;
}
public int getBalance() {return balance;
}}
ประกาศการเขาถงขอมลของตวแปรภายในคลาส
เปน private เพอใหสามารถเขาถงไดจากภายในคลาส
ประกาศการเขาถงขอมลของคอนสตรคเตอรของคลาสเปน public เพอให ผขอใชบรการจากวตถทถกสรางขนจากวตถนสามารถสรางวตถนได
ประกาศการเขาถงขอมลของเมทธอดเปน public
เพอใหเมทธอดของคลาสสามารถใหบรการแกผขอ
ใชบรการนอกขอบเขตของคลาส
An Example of Access Control
Note : กรณทประกาศการเขาถงขอมลของคลาสเปน public คลาสนน
จะตองถกบนทกในไฟลทมอชอเดยวกบคลาส “BankAccout.java” แยกจาก
ไฟลทระบผสรางวตถจากคลาส หรอขอรบบรการจากคลาสน
public
63อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 64: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/64.jpg)
Inheritance & Access Control
การระบคาการเขาถงขอมล(Access Control) ใหเปน publicทาใหคลาสอนๆ ทกคลาส สามารถเขาถง Attributes ของคลาสEmployee ได
ถาตองการใหเฉพาะคลาสManager สามารถเขาถงAttributes ของ Employee จะทาอยางไร?
Employee
+ name : String+ salary : double+ birthDate : MyDate
+ getDetails( ) : String
Manager
- department : String
พจารณา
64อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 65: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/65.jpg)
Inheritance & Access Control
สามารถกาหนดเขาถงขอมล (Access Control) ใหสามารถเขาถงไดผานทางการสบทอดคณสมบต เทาน น
ทาไดโดยระบคาAccess Control Specifier เปนprotected
พจารณา
Employee
# name : String# salary : double# birthDate : MyDate
- getDetails( ) : String
Manager
- department : String
protected
65อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 66: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/66.jpg)
Employee & Manager Example
public class Manager extends Employee {private String department;public String getDetails( ) {
return super.getDetails( ) + ", departmentt:"+department);}
}
public class Employee {protected String name;protected double salary;public String getDetails( ) {
return ("name:"+name+ ”, salary:"+salary);}
}
66อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 67: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/67.jpg)
“Point” Classpublic class public class Point {
protected int x,y;public Point() {
setPoint(0, 0);}public Point(int a, int b) {
setPoint(a, b);}public void setPoint(int a, int b) {
x = a;y = b;
}public int getX() { return x; }public int getY() { return y; }public String toString() { return "("+x+","+y+")"; }
}
67อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 68: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/68.jpg)
“Circle” Classpublic class Circle extends Point {
protected double radius;public Circle() {
setRadius(0.0);}public Circle(double r, int a, int b) {
super(a , b);setRadius(r);
}public void setRadius(double r) { radius = r; }public double getRadius() { return radius; }public double area() { return Math.PI * radius * radius; }public String toString() {
return "Center = "+"("+ x +","+ y +")" +"; Radius = "+ radius;
}}
68อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 69: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/69.jpg)
“Cylinder” Classpublic class Cylinder extends Circle {
protected double height;public Cylinder() { setHeight(0); }public Cylinder(double r, double h, int a, int b) {
super(r, a , b);setHeight(h);
}public void setHeight(double h) { height = h; }public double getHeight() { return height; }public double area() {
return 2 * super.area() +2 * Math.PI * radius * height; }public String toString() {
return super.toString()+ "; Height = " + height;}
}
69อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 70: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/70.jpg)
“TestInheritance” Classpublic class TestInheritance {
public static void main(String[] args) {Cylinder c = new Cylinder(5.7, 2.5, 12, 23);System.out.println("X coordinate is "+ c.getX() +
"\nY coordinate is "+ c.getY() +"\nRadius is "+ c.getRadius() +"\nHeight is ”+c.getHeight() +"\nCylinder Area = "+ c.area());
c.setHeight(10);c.setRadius(4.25);c.setPoint(2,2);System.out.println("\n\nThe new Location : "+
"\nX coordinate is "+ c.getX() +"\nY coordinate is "+ c.getY() +"\nRadius is "+ c.getRadius() +"\nHeight is ”+c.getHeight() + "\nCylinder Area = "+ c.area());
}70อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 71: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/71.jpg)
Inheritance Relationships of
Point, Circle and Cylinder
Circle
# radius : double
+ Circle()+ Circle(r:double, a:int, b:int)+ setRadius(r:double)+ getRadius() : double+ area() : double+ toString() : String
Cylinder
# height : double
+ Cylinder()+ Cylinder(r:double, h:double, a:int, b:int)+ setHeight(h:double)+ getHeight() : double+ area() : double+ toString() : String
Point
# x : int# y : int
+ Point()+ Point(a:int, b:int)+ setPoint(a : int, b : int)+ getX() : int+ getY() : int+ toString() : String
71อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 72: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/72.jpg)
Object Diagram of TestInheritance
TestInheritance
main
Cylinderx = 12
c
radius = 5.7
height = 2.5
getX
y = 23
getY
getRadius
getHieght
setRadius
setHeight
setPoint
area
toString72อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 73: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/73.jpg)
Specialization Subclass/Child Class รบถายทอดคณสมบต (inherit) ท งAttributes และ Methods มาจาก Superclass/Parent Class
Employee
+ name : String+ salary : double+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : StringManager
Employee
73อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 74: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/74.jpg)
Generalization
Employee
+ name : String+ salary : double+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
เปน flip-side ของ Specializationการท class ทตางกนม Attributes และ Methods ทเหมอนกน
IS - A
Employee
Manager
74อ.ดร. นฐพงศ สงเนยม
Aggregrate : Has a
23/01/61
![Page 75: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/75.jpg)
Class HierarchiesPoint
Circle Square
Cylinder Sphere Box Cubic
ลกศรใน Class Hierararchies แสดงความสมพนธของการรบถายทอดคณสมบต
Cylinder extends Circle Circle extends Point
75อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 76: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/76.jpg)
อ.ดร. นฐพงศ สงเนยม 7623/01/61
![Page 77: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/77.jpg)
อ.ดร. นฐพงศ สงเนยม 7723/01/61
![Page 78: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/78.jpg)
IS-A Relationships การถายทอดคณสมบตเปนคณลกษณะแบบ transitive
Snake IS-A Reptile และ Reptile IS-A Animal นนคอ Snake IS-A Animal
Animal
Reptile Mammal
Snake Lizard Dog Bat
Bird
Eagle
IS-A
IS-A
78อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 79: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/79.jpg)
Adopting a Design การออกแบบ Class Hierarchies ทเหมาะสม เปนสงสาคญการการออกแบบระบบเชงวตถ Alternatives Hierarchies ขนกบระบบทตองการออกแบบ
Animal
Mammal
Dog Bat
Bird
Eagle
หรอFlying Creature
BirdMosquito Bat
Eagle
79อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 80: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/80.jpg)
The “Object”Class ในภาษาจาวา
คลาส “Object” เปนคลาสบนสดของ คลาสทงหมดทประกอบกนขนเปน Class Hierarchies คลาสทประกาศ โดยไมใชคาเฉพาะ “extends” ระบการสบทอดคณสมบตจากคลาสใดๆ ถอวาเปนการละ “extends Object” ไว
public class Employee {...
}
public class Employee extends Object {...
}
เหมอนก
บ
80อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 81: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/81.jpg)
Methods defined in “Object”Class
Methods ทถกกาหนดไวในคลาส “Object” ใหคลาสทรบสบทอดคณสมบตสามารถเรยกใช หรอทาการ Overriding Methods เหลาน ไดตวอยางเชน
equals(Object object) : Indicates whether some other object is "equal to" this one. toString() : Returns a string representation of the object Clone() : Creates and returns a copy of this object.
...etc. ดรายละเอยดใน Java & API Language Specification
81อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 82: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/82.jpg)
Two Approaches to reuse Software
Inheritance
IS-A relationship
Composition
HAS-A relationship
HAS-ACustomer BankAccount
Employee
Manager
IS-A
82อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 83: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/83.jpg)
Benefits of Inheritance Avoiding redundancies
Source Code ในสวนทเหมอนกนจะถกเขยนขนเพยงครงเดยว Code reuse & Code Sharing
สามารถนาไปใชใหมได (reuse) ไดโดยการรบถายทอดคณสมบต สามารถสรางคลาสใหมจากคลาสเดมทมอยแลวได
Reduced code size Source Code ถกเขยนขนเพยงครงเดยว และถกนาไปใชใหมไดไม
จากด
83อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 84: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/84.jpg)
Costs of Inheritance Message-passing overhead เนองจากอนญาตใหม Code Reuse ทาใหตองมการสงตอ
ความตองการไปยงคลาสอนๆ ทสมพนธกนดวยการถายทอดคณสมบต
Execution Speed ความเรวในการประมวลผลชาลง เนองจากตองสงตอความ
ตองการไปขอรบบรการจากคลาสอนๆ Program Complexity เพมความซบซอนของโปรแกรม
84อ.ดร. นฐพงศ สงเนยม23/01/61
![Page 85: Lec09 Inheritancesiam2dev.net/E_Learning/OOP/Lec09_Inheritance_last...Lec09 Inheritance อ.ดร. น ฐพงศ ส งเน ยม Website Email : xnattapong@hotmail.com Last](https://reader034.fdocuments.co/reader034/viewer/2022050522/5fa54ff081a27e67540abc95/html5/thumbnails/85.jpg)
Summary• Overloading Methods and Constructors• Programming with multiple classes• Inheritance• Overriding Methods and Constructor• Access control and inheritance
85อ.ดร. นฐพงศ สงเนยม23/01/61