Java Class Design - http://www.siam2dev.com

Download Report

Transcript Java Class Design - http://www.siam2dev.com

:: OOP ::
Object Oriented Programming
Lec10 :: Overloading
By Nattapong Songneam
http://www.siam2dev.com
Information technology
Rajabhat pranakhon university
•Class Design in Java
Overloading
Programming with Multiple Classes
Inheritance
Inheritance and Access Control
Nattapong Songneam
Overloading Methods
 การประกาศเมทธอดซา
้ ซ้อน (Overloading Methods)
 คือ วิธีการเชิงวัตถุ ที่มีไว้เพื่อนาชื่อเมทธอด
(Methods Name) กลับมาใช้ใหม่ (reuse)
 ภาษาโปรแกรมจาวาสนับสนุน Overloading Methods
โดย
 อนุpublic
ญาตให้
ประกาศเมทธอดที
่มีชื่อเดียวกัน
void deposit(double
amount);
public void deposit(int amount);
voidม
deposit(double
amount,
double interest);
ซา้ กันpublic
ได้
แต่
ี
เ
งื
อ
่
นไขดั
ง
นี
้
public boolean deposit(double amount);
 ชนิด หรือ จำนวน หรือ ลำดับของ argument
Overloading Constructors
 การประกาศคอนสตรัคเตอร์ซา
้ ซ้อน (Overloading
Constructors)
คือ วิธีการเชิงวัตถุ ที่มีไว้เพื่อเพิ่มความ
ยืดหยุ่นให้กบั การสร้างวัตถุ กรณี ที่ไม่สามารถ
กาหนดข้อมูลเริ่มต้นที่แน่ นอนในขณะสร้าง
วัตถุ
public BankAccount (double initAmount) {/*…*/ }
public BankAccount (String customerName) {/*…*/ }
 public
ภาษาโปรแกามจาวาสนั
บสนุน Overloading
BankAccount ( ) { /*…*/ }
public BankAccount (double initAmount, String customerName) {/*…*/ }
Constructors โดย

An Example of Overloading
กำรกำหนดคอนสรัคเตอรซ
์
ซ้อน มีประโยชนในกรณี
ท
์
ไมสำมำรถก
ำหนดคำจ
public OverLoadedBankAccount( ) {
่
่ ำนวน
balance = 0.0;
เงินฝำกเริม
่ ตนเมื
อ
่ เปิ ดบัญช
้
}
ทำให้กำรใช้งำนโปรแกรม
public OverLoadedBankAccount(double intBalance) { มีควำมยืดหยุน
่
public class OverLoadedBankAccount {
private double balance;
balance = initBalance;
}
นำชือ
่ เมทธอดกลับมำใช้ไหม่
โดยระบุ “ชนิด” ของ Argument
เป็ นประเภทขอมู
่ ตกตำงกั
น
้ ลทีแ
่
public void deposit(int amount) {
balance = balance + amount;
}
public void deposit(double amount) {
balance = balance + amount;
}
}
BankAccount Example
public class OverLoadedAccountTester {
public static void main(String[] args) {
BankAccount myAccount =
new BankAccount2( );
BankAccount yourAccount =
new BankAccount2(100.0);
myAccount.deposit(5000.50);
myAccount.withdrawn(2000);
System.out.println("my balance:"
+myAccount.getBalance());
yourAccount.deposit(300.50);
yourAccount.withdrawn(200);
System.out.println("your balance:”
+ yourAccount.getBalance());
public class BankAccount2 {
private double balance;
public BankAccount2( ) {
balance = 0.0;
}
public BankAccount2(double intBalance) {
balance = initBalance;
}
public void deposit(int amount) {
balance = balance + amount;
}
public void deposit(double amount) {
balance = balance + amount;
}
}
}
:
OverLoadedTester.java
OverloadedBankAccount.java
Programming with
Multiple Classes
Class Diagram with Multiple Classes
Customer
BankAccount
account
- firstName : String
- lastName : String
- account : BankAccount
+
+
+
+
+
1
Customer(f:String, l:String)
getFirstName : String
getLastName : String
setAccount( acct:BankAccount)
getAccount( ) : BankAccount
Aggregation
- balance : double
Association
Name
+
+
+
+
BankAccount(initBalance:double)
getBalance : double
deposit(amt : double)
withdraw(amt : double)
Direction
indicator
Multiplicity
Class Diagram of “Customer.java” and “BankAccount.java”
Customer Class
public class Customer
{
private String firstName;
private String lastName;
private BankAccount account;
public Customer(String f, String l) {
this.firstName = f;
this.lastName = l;
this.account = null;
}
public String getName() {
return (this.firstName + " " + this.lastName);
}
public BankAccount getAccount() { return this.account; }
public void setAccount(BankAccount acct) { this.account = acct; }
}
BankAccount Class
public class BankAccount {
private double balance = 0.0;
public BankAccount(double amount) {
balance = amount;
}
public void deposit(double amount) {
balance = balance + amount;
}
public void withdrawn(double amount) {
balance = balance - amount;
}
public double getBalance() {
return balance;
}
}
Programming with Multiple Classes
public class TestBanking {
public static void main(String[] args) {
Customer cust = new Customer("Joe","Goodman");
cust.setAccount(new BankAccount(3000.0););
System.out.println("customer : " + cust.getName()
+ " : open account with balance = "
+ cust.getAccount().getBalance() + " baht.");
cust.getAccount().deposit(1250.25);
System.out.println("customer : " + cust.getName()
+ " : deposit 1250.25 baht :"
+ " current balance = "
+ cust.getAccount().getBalance() + " baht.");
}
}
Programming with Multiple Classes
Object Diagram of
“TestBanking.java” “Customer.java”
and “BankAccount.java”
TestBanking
main
cust
Customer
firstName=“John”
lastName=“Goodman”
Class Diagram
account=null
MyDate.java
getName
getAccount
setAccount
After Line 3
Programming with Multiple Classes
TestBanking
main
acct
cust
Executing
Line 4
Customer
firstName=“John”
lastName=“Goodman”
account=acct
account=null
acct
BankAccount
balance=3000.0
getName
deposit
getAccount
withdraw
setAccount
getBalance
Class Diagram
MyDate.java
Programming with Multiple Classes
TestBanking
main
cust
After Line 4
Customer
firstName=“John”
lastName=“Goodman”
account=acct
acct
BankAccount
balance=3000.0
getName
deposit
getAccount
withdraw
setAccount
getBalance
Programming with
Multiple Classes
TestBanking
main
deposit(1250.25)
cust
Executing
Line 6
Customer
acct
firstName=“John”
BankAccount
lastName=“Goodman”
balance=4250.25
balance=3000.0
account=acct
1250.25
getName
deposit
getAccount
withdraw
setAccount
getBalance
Programming with Multiple Classes
TestBanking
main
cust
Customer
firstName=“John”
lastName=“Goodman”
account=acct
After Line 6
acct
BankAccount
balance=4250.25
getName
deposit
Class Diagram
getAccount
withdraw
MyDate.java
setAccount
getBalance
Class Diagram with Multiple Classes
Customer
- firstName : String
- lastName : String
- account : BankAccount []
+
+
+
+
+
BankAccount
account
Customer(f:String, l:String)
getFirstName : String
getLastName : String
setAccount( acct:BankAccount)
getAccount( ) : BankAccount
“account” is
an array of BankAccount
- balance : double
*
+
+
+
+
BankAccount(initBalance:double)
getBalance : double
deposit(amt : double)
withdraw(amt : double)
Multiplicity
Class Diagram of “Customer.java” and “BankAccount.java”
Inheritance
Inheritance
การรับถ่ายทอด
คุณสมบัติ (Inheritance)
 หมายถึง การที่
คลาสหนึ่ งๆ รับถ่าย
คุณสมบัติ (inherit) ทัง้ ค่า
คุณลักษณะ (Attribute)
และพฤติกรรม (Method)
มาจากอีกคลาสหนึ่ ง

Employee
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
Inheritance
BankAccount
- balance : double
+BankAccount(intibalance:double)
+getBalance( ) : double
+deposit(amount : double)
+withdraw(amount : double)
SavingsAccount
- interestRate : double
รับถำยทอด
่
คุณสมบัต ิ
(inherit)
+SavingsAccount(intibalance:doub
le, rate:double)
Class Diagram of “BankAccount” class and “SavingsAccount” class
Subclassing

พิจารณาคลาส Employee
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
public class Employee {
public String name;
public double salary;
public MyDate birthDate;
+ getDetails( ) : String
public String getDetails( ) { … }
}
เราสามารถสร้างวัตถุที่มีคลาสเป็ น
พนักงาน ได้โดยการกาหนดคลาส “Employee”
 ถ้าเราต้ องการสร้างคลาสของ “Manager” ที่

Subclassing

พิจารณาคลาส Manager
public class Manager {
Manager
+
+
+
+
public String name;
public double salary;
public MyDate birthDate;
public String department;
name : String
salary : double
birthDate : MyDate
department : String
public String getDetails( ) { … }
+ getDetails( ) : String
}
โครงสร้างคลาส “Manager” กับ “Employee”
ซา้ ซ้อนกัน
 เราใช้ วิธีการเชิงวัตถุสร้างคลาสใหม่ จาก

Subclassing
 UML Diagram
Employee
Superclass
(Parent Class)
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Manager
Subclass
(Child Class)
+ department : String
การสร้างคลาสใหม่จากคลาสเดิมที่มีอยู่
แล้ว สนับสนุนแนวคิด ของการนา Source

Inheritance

การรับถ่ายทอดคุณสมบัติ
(Inheritance)
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
หมายถึง การที่คลาส
หนึ่ งๆ รับถ่ายคุณสมบัติ
(inherit) ทัง้ ค่าคุณลักษณะ
(Attribute) และพฤติกรรม
มาจากอี
คลาส
• (Method)
คือกำรที
่ class ทีต
่ กำงกั
นมี Attributes และ Methods ที่
่
เหมื่ งอนกัน
หนึ

+ getDetails( ) : String
Manager
+ department : String
Single Inheritance
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Manager
+ department : String
 Single Inheritance
หมายถึง การที่คลาส
หนึ่ งๆ รับถ่ายทอด
คุณสมบัติ (inherit) ทัง้ Attribute
และ Method มาจากอีกคลาส
หนึ่ ง
 เราเรียกว่าคลาส
“Manager” รับถ่ายทอด

Multiple Inheritance
 Multiple Inheritance
หมายถึง การที่คลาสหนึ่ งๆ รับถ่ายทอด
คุณสมบัติ (inherit) ทัง้ ค่าคุณลักษณะ และ
 ภาษาจาวา
พฤติกรรมมาจากคลาสมากกว่า 1 คลาส
 ไม่สนับสนุน
การทา Multiple

Employee
People
+ Name : String
+ id : String
+ birthDate : MyDate
+ company : String
+ salary : double
+ startDate : MyDate
+ getDetails( ) : String
+ getDetails( ) : String
Inheritance
Manager
+ department : String
แต่สามารถรับ
สืบทอด

Java Inheritance
 UML Diagram
Employee
+ name : String
+ salary : double
+ birthDate : MyDate

จาวา
public class Employee {
public String name;
public double salary;
public MyDate birthDate;
+ getDetails( ) : String
public String getDetails( ) { … }
}
Manager
+ department : String
public class Manager
extends Employee {
public String department;
}
Defining Extending Class in Java

การกาหนดคลาสที่รบั คุณสมบัติสืบทอดใน
ภาษาจาวา สามารถทาได้ดงั นี้
<modifier> class child-class
extends parent-class {
:
}
 ตัpublic
วอย่class
าง Manager extends Employee {
public String department;
}
“super” and “this”
keyword
 “this” keyword
ใช้อ้างอิงถึงคลาสปัจจุบนั
 สามารถใช้อ้างอิงถึง Attribute หรือ Method
ของคลาสปัจจุบนั ได้โดยใส่ . แล้วตามด้วย
ชื่อ Attribute หรือ ชื่อ Method

 “super” keyword

ใช้อ้างอิงถึง superclass ของคลาสปัจจุบนั
 super( ) อ้างอิงถึง constructor ของ superclass
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 Tester1 {
public static void main(String[] args) {
new Bottom();
}
}
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();
}
}
Overriding Methods
สามารถ
เปลี่ยนแปลงพฤติกรรม ที่
รับถ่ายทอด (inherit) มาจาก
superclass/parent class ได้
 subclass สามารถกาหนด
Method ที่ มีหน้ าที่ /
พฤติกรรม ต่ำงจำก Method
ของ superclass ได้แต่ต้องมี
 subclass/child class
Employee
+ name : String
+ salary : double
+ birthDay : MyDate
+ getDetails( ) : String
Manager
- department : String
+ getDetails( ) : String
Overriding Method (1)
public class Employee {
public String name;
public double salary;
public String birthDate;
public Employee(String n, double s, MyDate bd) {
name = n;
salary = s;
birthDate = bd;
}
public String getDetails( ) {
return "name:"+name+",salary:"+salary
+"bd:"+birthDate.toString();
}
}
Overriding Methods (2)
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 "name:"+name+",salary:"+salary
+"bd:"+birthDate.toString() +
+", department:"+this.department;
}
}
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;
}
}
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;
}
}
TestCompany
public 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”
Employee & Manger
Example
TestCompany
main
jeff
rob
Employee
name=“Jeffrey”
Salary=1000.0
birthday=jeffdate
getDetails
Manager
jeffdate
name=“Robert”
robdate
MyDate
Salary=5000.0
MyDate
day=2;
birthday=Robdate
day=3;
month=3;
month=4;
department=“Sales”
year=1970;
setDate
toString
getDetails
year=1965;
setDate
toString
Access Control

การควบคุมการเข้าถึงข้อมูล (Access Control)
 คือ วิธีการเชิงวัตถุ ที่ภาษาจาวามีไว้เพื่อ
ใช้เขียนโปรแกรมสนับสนุนคุณสมบัติ
Encapsulation และ Information Hiding
 การระบุค่า Access Control Specifier เป็ นการ
ควบคุมการเข้าถึงข้อมูล โดยจะใช้ระบุก่อน
หน้ า ตัวแปร ค่ำคงที ่ เมทธอด และคลำส
 ค่า Access Control Specifier ได้แก่
An Example of Access Control
public class BankAccount {
private double balance;
ประกำศกำรเขำถึ
้ งขอมู
้ ล
ของคอนสตรัคเตอรของ
์
ประกำศกำรเขำถึ
ง
ข
อมู
ล
้
้
public BankAccount(double initAmount)
{
คลำสเป็
น public เพือ
่ ให้
ของตัวแปรภำยในคลำส balance = initAmount;
ผู้ขอใช้บริกำรจำกวัตถุท
เป็ น private เพือ
่ ให้สำมำรถ}
ถูกสรำงขึ
น
้ จำกวัตถุนี้
้
ขำถึ
ง
ได
จำกภำยในคลำส
้
้
public void deposit(double amount) {
สำมำรถสรำงวั
้ ตถุนี้ได้
balance = balance + amount;
}
ประกำศกำรเขำถึ
ง
ข
อมู
ล
้
้
Note : กรณีทป
ี่ ระกำศกำรเขำถึ
ของเมทธอดเป็ น public public void withdrawn(double
้ ง
amount) {
ขอมู
ลของคลำสเป็ น public คลำสนั้น
เพือ
่ ให้เมทธอดของคลำส balance = balance
้ - amount;
}
จะตองถู
กบันทึกในไฟลที
่ อ
ื ชือ
่ เดียวกับ
สำมำรถให้บริกำรแกผู
้
์ ม
่ ้ขอ
คลำส “BankAccout.java” แยกจำก
ช้บริกำรนอกขอบเขตของpublic int getBalance()
{
ู ำงวั
คลำส
return balance;ไฟลที
้ ตถุจำกคลำส หรือ
์ ร่ ะบุผ้สร
}
ขอรับบริกำรจำกคลำสนี้
}
Inheritance & Access Control

พิจารณา
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Manager
- department : String
การระบุค่าการ
เข้าถึงข้อมูล (Access
Control) ให้เป็ น public ทา
ให้คลาสอื่นๆ ทุก
คลาส สามารถเข้าถึง
Attributes ของคลาส
Employee ได้

Inheritance & Access Control

พิจารณา
Employee
# name : S tring
# salary : double
# birthD ate : M y D ate
- getD etails( ) : S tring
protected
M anager
- department : S tring
สามารถกาหนด
เข้าถึงข้อมูล (Access
Control) ให้สามารถ
เข้าถึงได้ผา่ นทางการ
สืบทอดคุณสมบัติ
เท่านัน้
 ทาได้โดยระบุ

Employee & Manager Example
public class Employee {
protected String name;
protected double salary;
public String getDetails( ) {
return ("name:"+name+ ”, salary:"+salary);
}
}
public class Manager extends Employee {
private String department;
public String getDetails( ) {
return super.getDetails( ) + ", departmentt:"+department);
}
}
“Point” Class
public 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+")"; }
}
“Circle” Class
public 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;
}
}
“Cylinder” Class
public 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;
}
}
“TestInheritance” Class
public 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());
}
Inheritance
Relationships
of
Point, Circle and
Cylinder
Point
# x : int
# y : int
+
+
+
+
+
+
Point()
Point(a:int, b:int)
setPoint(a : int, b : int)
getX() : int
getY() : int
toString() : String
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
Object Diagram of TestInheritance
TestInheritance
main
c
Cylinder
x = 12
radius = 5.7
y = 23
height = 2.5
getX
setRadius
getY
setHeight
getRadius
setPoint
getHieght
area
toString
Specialization
รับถ่ายทอดคุณสมบัติ (inherit)
ทัง้ Attributes และ Methods มาจาก Superclass/Parent Class
 Subclass/Child Class
Employee
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Emplo
yee
Manager
+ department : String
Mana
ger
Generalization
เป็ น flip-side ของ Specialization
การที่ class ที่ ต่างกันมี Attributes และ Methods ที่
เหมือนกัน

Employee
+ name : String
+ salary : double
+ birthDate : MyDate
+ getDetails( ) : String
Emplo
yee
IS A
Manager
+ department : String
Mana
ger
Class Hierarchies
Point
Circle
Cylinder
Square
Sphere
Box
ลูกศรใน Class Hierararchies แสดง
ความสัมพันธ์ของการรับถ่ายทอด
คุณสมบัติ

 Cylinder extends Circle
Cubic
IS-A Relationships
การถ่ายทอดคุณสมบัติเป็ นคุณลักษณะ
แบบ transitive
 Snake IS-A Reptile และ Reptile IS-A Animal
Animal
 นัน
่ คือIS-ASnake IS-A Animal

IS-A
Snake
Reptile
Bird
Lizard
Eagle
Mammal
Dog
Bat
Adopting a Design
การออกแบบ Class Hierarchies ที่เหมาะสม
เป็ นสิ่งสาคัญการการออกแบบระบบเชิง
วัตถุ
Animal
Flying Creature
 Alternatives Hierarchies ขึน
้ กับระบบที
่ต้องการ
หรื
ออกแบบ Mammal
Bird
อ Mosquito Bird

Eagle
Dog
Bat
Eagle
Bat
The “Object”Class
ในภาษาจาวา
 คลาส “Object” เป็ นคลาสบนสุดของ คลาส
ทัง้ หมดที่ประกอบกันขึน้ เป็ น Class Hierarchies
 คลาสที่ ประกาศ โดยไม่ใช้คาเฉพาะ
ิ
“extends” ระบุการสืบpublic
ทอดคุ
ณ
สมบั
ต
จ
าก
class
Employee
extends
Object {
public class Employee { เหมือ
...
...
คลาสใดๆ ถืนกั
อว่บาเป็} นการละ “extends Object”
}
ไว้

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.
Two Approaches to reuse Software

Inheritance
 IS-A relationship
Employee
IS-A
Manager

Composition
 HAS-A relationship
Customer
BankAccount
HAS-A
Benefits of Inheritance

Avoiding redundancies
 Source Code ในส่วนที่ เหมือนกันจะถูกเขียน
ขึน้ เพียงครัง้ เดียว

Code reuse & Code Sharing
สามารถนาไปใช้ใหม่ได้ (reuse) ได้โดยการ
รับถ่ายทอดคุณสมบัติ
 สามารถสร้างคลาสใหม่จากคลาสเดิมที่
มีอยู่แล้วได้


Reduced code size
Costs of Inheritance

Message-passing overhead
เนื่ องจากอนุญาตให้มี Code Reuse ทาให้ต้อง
มีการส่งต่อความต้องการไปยังคลาสอื่นๆ
ที่สมั พันธ์กนั ด้วยการถ่ายทอดคุณสมบัติ


Execution Speed
ความเร็วในการประมวลผลช้าลง
เนื่ องจากต้องส่งต่อความต้องการไป
ขอรับบริการจากคลาสอื่นๆ


Program Complexity
Summary
•
•
•
•
•
Overloading Methods and Constructors
Programming with multiple classes
Inheritance
Overriding Methods and Constructor
Access control and inheritance