Professional Documents
Culture Documents
Chapter 8
Chapter 8
class ทีเ่ ปนตนแบบ จะเรียกวา super-class หรือ based-class หรือ parent-class สวน class ที่
แตกออกมาจาก class ตนแบบ เรียกวา sub-class หรือ derived-class หรือ child-class ในการสืบทอด sub-
class จะมีคุณสมบัติรวมเหมือนกับ super-class
2/2545 1
CS313: Object-Oriented Programming บทที่ 8: Object-Oriented Programming
บัญชีธนาคาร
getBalance()
deposit()
widthdraw()
บัญชีออมทรัพยพิเศษ
minWithdrawPerMonth()
2/2545 2
CS313: Object-Oriented Programming บทที่ 8: Object-Oriented Programming
class Y extends X {
public int varYPublic = 0;
protected int varYProtected = 0;
private int varYPrivate = 0;
void methodY() {
varYPublic = 5;
varYProtected = 5;
varYPrivate = 5;
System.out.println(“In subclass Y”);
System.out.println(“Access Protected super X ” + varXProtected);
// System.out.println(“Access Protected super X ” + varXPrivate);
}
2/2545 3
CS313: Object-Oriented Programming บทที่ 8: Object-Oriented Programming
class TestAccessMember {
public static void main (String args[] ) {
X objX = new X();
Y objY = new Y();
System.out.println(“Test private case”);
// System.out.println(“Access super X ” + objX.varXPrivate);
// System.out.println(“Access subclass Y ”+objY.varYPrivate);
// objY.methodY();
System.out.println(“Test public case”);
System.out.println(“Access super X ”+objY.varXPublic);
System.out.println(“Access subclass Y ”+objY.varYPublic);
คําถามชวนคิด
ในตัวอยางที่ 8.1 ใหนักศึกษาลองเอา comment ออก ในบรรทัดที่ถูก comment ไว แลวดูผลลัพธของโปรแกรมวา
เปนอยางไร และทําไมจึงเปนเชนนั้น
ในการประกาศ method ชื่อ ProtectedMethodX ของ class Y พบวา method นีม้ ชี ื่อเหมือนกับ
method ใน class X เพือ่ มิใหเกิดความสับสนในการเรียกใช method หรือ variable ทีม่ ชี ื่อซํ้ากันทั้งใน super-
class และ sub-class สามารถทําไดโดยใช this และ super เมื่อ this เปนการอางอิงถึง object ของ class ที่
กําลังประมวลผลอยู สวน super เปนการอางอิงถึง method หรือ variable ของ super-class
2/2545 4
CS313: Object-Oriented Programming บทที่ 8: Object-Oriented Programming
ตัวอยางที่ 8.2
class Point2D {
int x, y;
Point2D (int x, int y) { // Point2D1
this.x = x;
this.y = y;
}
Point2D () { // Point2D3
this(0,0);
}
}
Point3D() { // Point3D3
this(0,0,0);
}
}
class DemoThisSuper {
public static void main (String args[] ) {
Point3D d1 = new Point3D();
System.out.println(“x = “ + d1.x + “ y = “ + d1.y + “ z = “ + d1.z);
Point3D d2 = new Point3D(20,30,4);
System.out.println(“x = “ + d2.x + “ y = “ + d2.y + “ z = “ + d2.z);
}
}
2/2545 5
CS313: Object-Oriented Programming บทที่ 8: Object-Oriented Programming
8.3 Polymorphism
Shape
double x, y
String fname
Shape
getName
double calArea()
2/2545 6
CS313: Object-Oriented Programming บทที่ 8: Object-Oriented Programming
2/2545 7
CS313: Object-Oriented Programming บทที่ 8: Object-Oriented Programming
2/2545 8
CS313: Object-Oriented Programming บทที่ 8: Object-Oriented Programming
8.6 Interfaces
รูปแบบ
public interface InterfaceName extends SuperInterfaces {… Interface body … }
3. ไมจาเป
ํ นตองใสเครื่องหมาย { } access_modifier ของ method โดยปกติจะเปน public
abstract โดยอัตโนมัติ
ในการใชงาน interface ในภาษา Java นั้น class ตางๆ สามารถสืบทอดจาก super-class ไดเพียง
class เดียว แตการใช interface สามารถที่จะใชไดไมจํากัด
2/2545 9
CS313: Object-Oriented Programming บทที่ 8: Object-Oriented Programming
2/2545 10
CS313: Object-Oriented Programming บทที่ 8: Object-Oriented Programming
8.7 Packages
ในทางปฏิบัติ package ก็คือ directory นัน่ เอง เชน package java.awt.image เปนการอางอิง
directory c:\java\awt\image อยางไรก็ตามตัวแปรภาษา Java รุน ใหม ไมจําเปนตองแยกการเก็บ package
เปน directory ใหเห็นอยางชัดเจนเชนนี้ แตจะรวมไวในแฟมขอมูลประเภท jar แตการจัดหรือรวบรวม class
ในแฟม jar ยังคงใชหลักของการจัดการ directory ซึง่ สามารถตรวจสอบในขณะ compile โดยใส option -
verbose ในคําสั่ง javac
import package_name.class_name;
หรือ import package_name.*; // อางถึงทุก class ใน package_name นั้นๆ
2/2545 11
CS313: Object-Oriented Programming บทที่ 8: Object-Oriented Programming
package MyPack;
public class X {
public int varXPublic = 0;
protected int varXProtected = 0;
private int varXPrivate = 0;
package MyPack;
public class Y extends X {
public int varYPublic = 0;
protected int varYProtected = 0;
private int varYPrivate = 0;
import MyPack.*;
class TestAccessMember1 {
public static void main (String args[] ) {
X objX = new X();
Y objY = new Y();
System.out.println(“Test public case”);
System.out.println(“Access super X “+objY.varXPublic);
System.out.println(“Access subclass Y “+objY.varYPublic);
}
}
2/2545 12