3. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 183
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
Parent
Son
Super Class
Sub Class
A
B C
Method B Method C
Method A
การสืบทอด(Inheritance)
เราสามารถสรางคลาสใหมโดยอาศัยคลาสที่มีอยูแลวเปนตนแบบ เราเรียกคลาสใหมวา สับคลาส
(sub class) ของคลาสเกา และเรียกคลาสเกาวา ซูปเปอรคลาส(Supper class) ของคลาสใหม ตัวแปร
คลาสและ method จะไดรับการสืบทอดไปยังสับคลาสโดยอัตโนมัติ เหมือนกับการสืบทอดลักษณะทาง
พันธุกรรมจากพอไปสูลูก
ในภาษาจาวาจะยอมใหซับคลาสสืบทอด(inherited)คุณสมบัติไดจากซุปเปอรคลาสไดเพียงคลาส
เดียวเทานั้น ซึ่งไมเหมือนกับภาษา C++ ที่ซับคลาสสามารถสืบทอดคุณสมบัติไดจากหลายซุปเปอร
คลาส
กลาวไดวาคุณสมบัติการสืบทอด(Inheritance) คือการที่ Class หนึ่งๆสามารถสืบทอดคุณสมบัติ
บางประการจาก Class อื่น แลวเพิ่มคุณสมบัติเฉพาะของ Class นั้นเขาไป Class ที่ไดรับการสืบทอด
คุณสมบัติเรียกวา Subclasses สวน Class ที่เปนตนแบบเรียกวา Superclass ประโยชนของคุณสมบัติการ
สืบทอดคือ เปนการชวยใหไมตองออกแบบ พัฒนา สวนที่ซ้ําหลายๆรอบ(reusable) Class หนึ่งๆจะมี
Superclass ไดเพียง Class เดียวเทานั้น (Single Inheritance) ดังรูป
รูปแบบของการประกาศการสืบทอดคุณสมบัติของคลาส
class subclass_name extends superclass_name{
[member;]
}
4. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 184
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
ตัวอยาง โปรแกรมที่มีคุณสมบัติการสืบทอด
class A{
void printA(){
System.out.println("A");
}
}
class B extends A{
void printB(){
System.out.println("B");
}
}
class TestInherit1{
public static void main(String args[]){
A x = new A();
x.printA();
B y = new B();
y.printA();
y.printB();
}
}
ผลลัพธที่ได
A
A
B
ตัวอยาง โปรแกรมที่มีคุณสมบัติการสืบทอด
class AA{
int i,j;
void printA() {
System.out.println(" i = " + i + " : j = " + j);
}}
5. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 185
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
class BB extends AA{
int k;
void printB(){
System.out.println(" k = " + k);
}
void sum( ) {
System.out.println("i+j+k="+(i+j+k));
}
}
class TestInherit2{
public static void main(String args[]){
AA superClass = new AA();
BB subClass = new BB();
System.out.println("print from superclass");
superClass.i = 10;
superClass.j = 20;
superClass.printA();
System.out.println();
System.out.println("print from subclass");
subClass.i = 3;
subClass.j = 4;
subClass.k = 5;
subClass.printA();
subClass.printB();
subClass.sum();
}
}
ผลลัพธที่ไดคือ
print from superclass
i = 10 : j = 20
print from subclass
i = 3 : j = 4
k = 5
i+j+k=12
6. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 186
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
ตัวอยาง โปรแกรมที่มีการเรียกใชคลาสที่มีการสืบทอด
class TestInherit3{
static void methodA(A a){
a.printA();
}
static void methodB(B b){
b.printB();
}
public static void main(String args[]){
A x = new A();
methodA(x);
B y = new B();
methodA(y);
methodB(y);
A z = new B(); // super : sub
z.printA();
//z.printB();
}
}
/*method ที่รับ instance ของคลาสแมเปนพารามิเตอรจะสามารถรับ instance ของ class ลูกเปน
parameter ไดดวย*/
ผลลัพธที่ไดคือ
A
A
B
A
หมายเหตุ : หากตองการปองกันการเขาถึงdata member(attribute และ method)ของ super class ใหใส
สวนขยายหนา data member ดวยคําวา private
ตามนิยามของ Sun แบง ความสัมพันธระหวาง Classes เปนสองลักษณะคือ
– IS-A เปนความสัมพันธที่ Class หนึ่งเปน Subclass ของอีก Class หนึ่ง
– HAS-A เปนความสัมพันธที่ Class หนึ่งเปนสวนประกอบ ของอีก Class หนึ่ง
10. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 190
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
คลาสนี้มีตัวแปรคลาสสามตัวไดแก a , b และ c ในบรรทัด (1)(2)(3) method x() มีการสงผาน
ตัวแปรที่มีชื่อเหมือนตัวแปรคลาส a และ b ในบรรทัด (4) อีกทั้งยังประกาศตัวแปรภายในmethod ชื่อ c
ซึ่งซ้ํากับตัวแปรคลาสอีก ในบรรทัด (5) คําสั่งในบรรทัด (6) ใชคําสั่ง this ตามดวยจุดนําหนา a เปนการ
ชี้เฉพาะเจาะจงลงไปวา หมายถึงตัวแปรคลาส a ที่ประกาศไวในบรรทัด (1) คําสั่งนี้เปนการกําหนดคาตัว
แปรคลาส a ใหมีคาเทากับตัวแปรสงผาน a ซึ่งอยูหลังเครื่องหมายเทากับ ตัวแปรสงผานไมมีคําวา this
นําหนาคําสั่งในบรรทัด (7) เปนการอางถึงตัวแปร c ที่ประกาศไวในบรรทัด (5) เพราะไมมี this นําหนา
สรุปก็คือ ถามีการใชชื่อซ้ํากัน การใชชื่อตัวแปรเฉยๆ จะถือวาเปนตัวแปรภายใน method ถาตองการอาง
ถึงตัวแปรคลาสตองระบุ this ดวยเสมอ แตถาไมมีการใชชื่อซ้ํากัน ไมตองระบุ this ก็ไดเหมือนกันที่เรา
เคยเขียนกัน คําสั่ง this ใชระบุเฉพาะเจาะจงไดดวยวาหมายถึง method ในคลาสเดียวกันดังที่ใชใน
บรรทัด (8) แตเนื่องจากไมมีความสับสนในกรณีของ method อยูแลว (เพราะไมมี method ทองถิ่น) การ
ระบุ this จึงไมจําเปนสําหรับการเรียก method บรรทัด (8) กับ บรรทัด(9) จึงมีความหมายเหมือนกัน
ตัวอยาง การใช keyword this
แตถาเอาเครื่องหมาย // บรรทัดที่ (1) ออกแลวใสเครื่องหมาย // หนาบรรทัดที่(2) แทน ผลลัพธ
จะกลายเปน Pokemon แทน
class TestThis2 {
String name = "Pokemon";
TestThis2(String name){
//name = name; // (1)
this.name = name ;//(2)
}
void print(){
System.out.println(name);
}
public static void main(String[] args) {
TestThis2 t = new TestThis2("Sompong");
t.print();
}
}
ผลลัพธคือ
Sompong
11. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 191
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
ตัวอยาง การใช keyword this
คลาส TestThis3 ในตัวอยางนี้มี field r และ i และมี constructor รับพารามิเตอรที่ชื่อ r และ i
เขามา เพื่อกําหนดคาใหแก fields ที่มีชื่อเหมือนกัน ถาหากเขียนประโยคกําหนดคาเปน r = r และ i=i ก็
จะเปนการกําหนดคาใหแกตัวเองไมวาชื่อที่อางถึงนี้จะหมายถึงชื่อใด ซึ่งไมใชสิ่งที่ตองการ เราจึงตอง
ใช this ชวยในการระบุวา this.r หมายถึง r ตัวที่เปนสมาชิกในคลาส สวน r เพียงตัวเดียวนั้นคือ ตัวที่
เปนพารามิเตอร สังเกตวาชื่อพารามิเตอรจะบังชื่อที่อยูขางนอก method และในทํานองเดียวกัน this.i
กับ i หากใน method นั้นไมมีพารามิเตอรที่มีชื่อเหมือนกับ field ของคลาสอยางเชน add() และ print( )
ในตัวอยางนี้ การอางถึง r หรือ i จะหมายถึงตัวที่เปน field ของคลาส
นอกจากนั้นภาษาจาวา ยังมีการใช this ในอีกความหมายหนึ่งคือ ใช this ในการเรียก method
จะหมายถึงการเรียก constructor ของคลาสนั้น ดังตัวอยาง
this( )
class TestThis3 {
double r, ;
TestThis3(double r, double i ){
this.r = r ;
this.i = i;}
public void add(TestThis3 t){
r+= t.r ;
i += t.i;
}
void print(){
System.out.println(r + " + i " + i );
}
}
12. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 192
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
คงจําไดวาถาไมมีการกําหนดคาเริ่มตนของตัวแปรคลาสไว เวลาสรางอินสแตนท จาวาเวอรชัวน
แมทชีน จะกําหนดคาปกติให การกําหนดคาเริ่มตนทําไดดวยการกําหนดเขาไปเลยตอนนิยามคลาส จา
วามี method พิเศษชนิดหนึ่งชื่อวา คอนสตรัคเตอร ซึ่งเปน method ที่มีไวสําหรับการกําหนดคาเริ่มตน
ใหกับอินสแตนทโดยเฉพาะ คอนสตรัคเตอรเปนทางเลือกอีกทางหนึ่งของการกําหนดคาใหตัวแปรคลาส
ลองพิจารณาการกําหนดคาเริ่มตนใหตัวแปรคลาสดวยการใชคอนสตรัคเตอรดังตัวอยางขางลางนี้
ตัวอยาง การใชงาน constructor
Method constructor คือ method ในบรรทัด (1) method constructor ตองมีชื่อเหมือนชื่อคลาส
เสมอ แตตามดวย () เพื่อใหรูวาเปน method และตองไมมีคําวา void หรือชื่อชนิดของตัวแปรใดๆ
class Vehicle {
int numberOfWheels;
boolean hasEngine;
Vehicle() { // (1)
numberOfWheels = 6;
hasEngine = true;
}
void run(){
System.out.println("I am running");
}
}
public class BuildACar1 {
public static void main (String[] args) {
Vehicle myCar = new Vehicle();
System.out.println("My car has " + myCar.numberOfWheels + "wheels.");
System.out.println("That my car has an engine is " +myCar.hasEngine + ".");
}
}
ผลลัพธคือ
My car has 6wheels.
That my car has an engine is true.
13. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 193
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
นําหนาชื่อคอนสตรัคเตอร ภายในบลอคปกกาของคอนสตรัสเตอรเราใสคําสั่งกําหนดคาของตัวแปร
อินสแตนทลงไปเมื่อไรก็ตามที่มีการสรางอินสแตนทใหมของคลาส Vehicle ดวยคําสั่ง
Vehicle myCar = new Vehicle(); คําสั่งกําหนดคาตัวแปรอินสแตนทที่อยูในคอนสตรัคเตอรจะ
ทํางานทันที อันที่จริงคอนสตรัคเตอรก็เหมือนกับ method ทั่วไป คําสั่งที่อยูในบลอคปกกาไมจําเปนตอง
เปนคําสั่งกําหนดคาตัวแปรคลาสเสมอไป จะเปนคําสั่งอยางอื่นก็ได คําสั่งเหลานี้จะทํางานทุกครั้งที่มีการ
สรางอินสแตนทใหมใหคลาส ตัวอยางเชน
จากตัวอยางในบรรทัด (1) เราเติมคําสั่งใหโปรแกรมแสดงผลออกนอกจอวา A car has been
built ดังนั้นคําสั่งนี้จะถูกใชงานเมื่อมีการสราง instance myCar ขึ้น ซึ่งเกิดขึ้นกอนการแสดงจํานวนลอ
class Vehicle {
int numberOfWheels;
boolean hasEngine;
Vehicle() {
numberOfWheels = 6;
hasEngine = true;
System.out.println("A car has been built."); //(1)
}
void run(){
System.out.println("I am running");
}
}
public class BuildACar2 {
public static void main (String[] args) {
Vehicle myCar = new Vehicle();
System.out.println("My car has " + myCar.numberOfWheels + "wheels.");
System.out.println("That my car has an engine is " + myCar.hasEngine + ".");
}
}
ผลลัพธคือ
A car has been built.
My car has 6wheels.
That my car has an engine is true.
14. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 194
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
คอนสตรัคเตอรเปนmethod ดังนั้นคอนสตรัคเตอรสามารถสงผานตัวแปรไดดวย เชน
class Vehicle {
int numberOfWheels;
boolean hasEngine;
Vehicle() {
numberOfWheels = 6;
hasEngine = true;
System.out.println("A car has been built.");
}
Vehicle(int number, boolean engine) { // (1)
numberOfWheels = number;
hasEngine = engine;
System.out. println("A car has been built.");
}
void run(){
System.out.println("I am running");
}
}
public class BuildACar3 {
public static void main (String[] args) {
Vehicle myCar = new Vehicle(4,true); //(2)
System.out.println("My car has " + myCar.numberOfWheels + "wheels.");
System.out.println("That my car has an engine is " + myCar.hasEngine + ".");
}
}
ผลลัพธคือ
A car has been built.
My car has 4wheels.
That my car has an engine is true.
15. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 195
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
ในตัวอยางนี้เราเพิ่มคอนสตรัคเตอรที่มีการสงผานตัวแปรในบรรทัด (1) ซึ่งเปนการโอเวอร
โหลดคอนสตรัสเตอรที่มีอยูเดิมในบรรทัด (2) เราสรางอินสแตนทโดยใชคอนสตรัคเตอรตัวใหม ซึ่งมี
การสงผานตัวแปรดวย ผลที่ไดก็คือตัวแปรอินสแตนททั้งสองจะมีคาเหมือนกับตัวแปรที่เราสงผาน
รถยนตมี 4 ลอแทนที่จะมี 6 ลอ เราเรียกคอนสตรัคเตอรที่ไมมีการสงผานตัวแปรใดๆ วา คอน
สตรัสเตอรปกติ และเรียกคอนสตรัคเตอรที่มีการสงผานตัวแปรวา คอนสตรัคเตอรโอเวอรโหลด
This() กับ constructor
ภายในคอนสตรัคเตอรเราสามารถเรียกคอนสตรัคเตอรตัวอื่นในคลาสเดียวกันไดดวยการใช
คําสั่ง this() ดังตัวอยางตอไปนี้
class A {
int a; // (1)
A() {
a = 0;
System.out.println("Default Constructor");
}
A(int i) { // (2)
this();
a = i;
System.out.println("Constructor 1");
}
A(int i,int j) { // (3)
this(i+j);
System.out.println("Constructor 2");
}
}
public class TestThis4 {
public static void main(String[] args) {
System.out.println("Instantiate x");
A x = new A();
System.out.println("variable a is " + x.a);
System.out.println("Instantiate y");
A y = new A(5);
System.out.println("variable a is " + y.a);
System.out.println("Instantiate z");
A z = new A(5,6);
System.out.println("variable a is " + z.a);
}
}
16. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 196
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
จากตัวอยางและผลลัพธที่ได คลาส A มีสามคอนสตรัคเตอร คอนสตรัสเตอรแรกเปนคอน
สตรัสเตอรปกติในบรรทัด (1) กําหนดคาใหตัวแปรคลาส a เปนศูนย คอนสตรัคเตอรที่หนึ่งในบรรทัด
(2) มีการรับตัว แปร i และ มีการเรียกคอนสตรัคเตอรปกติดวยคําสั่ง this() กอนที่จะเปลี่ยนคาตั้งตนของa
ใหเทากับตัวแปร i สวนคอนสตรัคเตอรตัวสุดทายคือคอนสตรัคเตอรที่สองในบรรทัด (3) มีการเรียกคอน
สตรัสเตอรที่หนึ่งดวยคําสั่ง this(i+j) มันจะสงผานคา i+j ไปให a และเนื่องจากมันเรียกคอนสตรัคเตอรที่
หนึ่งซึ่งเรียกคอนสตรัคเตอรปกติ คอนสตรัคเตอร ปกติจึงถูกเรียกไปดวย
***คําสั่ง this() ใชไดภายในคอนสตรัคเตอรเทานั้นและตองเปนคําสั่งแรกสุดเสมอ***
ตัวอยาง การใชงาน this กับ constructor
class TestThis5 {
double r , i;
TestThis5(){ //(1)
this (0.0 , 0.0);
}
TestThis5(double r , double i){//(2)
this.r = r ;
this.i = i;
}
TestThis5(TestThis5 t) {//(3)
this(t.r,t.i);
}
ผลลัพธคือ
Instantiate x
Default Constructor
variable a is 0
Instantiate y
Default Constructor
Constructor 1
variable a is 5
Instantiate z
Default Constructor
Constructor 1
Constructor 2
variable a is 11
18. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 198
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
เราสามารถเรียก constructor ใน constructor โดยใช this () ไดเพียงครั้งเดียว ไมสามารถเรียกใช
งานครั้งที่สองได ดังเชนใน บรรทัดที่ (1) หากเอาเครื่องหมาย // ออก จะทําใหเกิด error
จากตัวอยางประกาศตัวแปร s ใน constructor Flower(String s, int petals) ซึ่งอาจทําให
compiler สับสนได จึงจําเปนตองใช this.s เพื่ออางถึง data member ใน class Flower และ s แทนชื่อตัว
แปรของ argument ดังในบรรทัดที่ (2)
ใน method print() เราไมสามารถเรียก ใชงาน constructor จาก method อื่นๆ ได ดังในหมายเลข
(3) หากนําเครื่องหมาย // ออกจะทําใหเกิด error
Super
เมื่อตองการเรียกคอนสตรัคเตอรของซุปเปอรคลาสใหทํางาน สามารถทําไดโดยใชคียเวิรด
super โดยมีขอกําหนดวา การเรียกใชงานคอนสตรัคเตอรของซุปเปอรคลาสจะตองทําการเรียกที่บรรทัด
แรกสุดของคอนสตรัคเตอรนั้นๆ เทานั้น
Flower() {
this("hi", 47);
System.out.println("default constructor (no args)");
}
void print() {
//! this(11); // Not inside non-constructor! (3)
System.out.println("petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
Flower x = new Flower();
x.print();
}
}
ผลลัพธคือ
Constructor w/ int arg only, petalCount= 47
String & int args
default constructor (no args)
petalCount = 47 s = hi
19. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 199
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
ตัวอยาง โปรแกรมการใชงานคียเวิรด this และ super
class A{
int i;
A( ){System.out.println("constructor A");}
}
class B extends A{
int i;
B(){
super();//default เรียกใช constructor ของ super
System.out.println("constructor B()");
}
B(int a,int b){
super.i = a;
this.i = b;
System.out.println("constructor B(int a, int b)");
}
void printB(){
System.out.println(" super = " + super.i);
System.out.println(" sub = " + this.i);
}
}
public class testSuper{
public static void main(String args[]){
B subB = new B(9,10);
subB.printB();
}
}
ผลลัพธที่ไดคือ
constructor A
constructor B(int a,int b)
super = 9
sub = 10
20. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 200
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
● ใน constructor ทุกตัวนั้นที่ Statement แรกจะตองระบุ constructor อื่นๆ ที่อยูใน class หรือ
superclass เสมอ หากไมระบุ compiler จะใส empty constructor ของ superclass ใหโดย
อัตโนมัติ
โปรแกรมที่สราง compiler สรางให
● ถา superclass ไมมี empty constructor แลว หากใน subclass ทําการเรียก super(); จะเกิด compile
error
โปรแกรมที่สราง compiler สรางให
public class A {
public A(int i){
}
}
class B extends A {
public B(){
}
}
public class A {
public A(int i){
}
}
class B extends A {
public B(){
super(); //Compiler จัดให
// Compile error
// แกโดย super(13);
}
}
=
public class A {
public A( ){
}
}
class B extends A {
public B( ){
}
}
public class A {
public A(){
}
}
class B extends A {
public B(){
super(); //Compiler จัดให
}
}
=
21. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 201
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
เมธอด instanceof
ใชสําหรับเปรียบเทียบวา Object นั้นๆสืบทอดมาจาก Class นั้นๆหรือไม
ตัวอยาง การใชงานเมธอด instanceof กับ คลาสที่มีการสืบทอด
class A { }
class B extends A { }
class Test{
public static void main (String [] args) {
A a = new B();
B b = new B();
A c = new A();
//B c = new A(); error
System.out.println("a is instance of A = " + (a instanceof A));
System.out.println("a is instance of B = " + (a instanceof B));
System.out.println("b is instance of A = " + (b instanceof A));
System.out.println("b is instance of B = " + (b instanceof B));
System.out.println("c is instance of A = " + (c instanceof A));
System.out.println("c is instance of B = " + (c instanceof B));
}
}
ผลลัพธที่ไดคือ
a is instance of A = true
a is instance of B = true
b is instance of A = true
b is instance of B = true
c is instance of A = true
c is instance of B = false
22. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 202
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
• นอกจากการสืบทอดแลว instance ยังใชกับการ implement ไดอีกดวย
ตัวอยาง การใชงานเมธอด instanceof กับ interface ซึ่งจะกลาวในหัวขอถัดไป
interface Foo { }
class Bar implements Foo { }
class TestBar {
public static void main (String [] args) {
Bar b = new Bar();
if ( b instanceof Bar) {
System.out.println("b is a Bar");
}
if (b instanceof Foo) {
System.out.println("b is a Foo");
}
}
}
ผลลัพธที่ไดคือ
b is a Bar
b is a Foo
การสืบทอดคุณสมบัติของคลาสที่มีมากกวา 2 ระดับ
Class A
Class B
Class C
23. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 203
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
ตัวอยาง โปรแกรมที่ไดจากรูปคือ
class A{
A(){
System.out.println("Print from A");
}
}
class B extends A{
B(){
System.out.println("Print from B");
}
}
class C extends B{
C(){
System.out.println("Print from C");
}
}
class testInherit5{
public static void main(String args[]){
C subC = new C();
}
}
ผลลัพธที่ไดคือ
Print from A
Print from B
Print from C
24. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 204
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
แบบฝกหัด
1. ถากําหนดใหคลาส Dog มีโครงสรางดังนี้
class Dog {
Dog(String name) { }
}
แลวคลาส Beagle สืบทอด(inherited)คุณสมบัติจากคลาส Beagle มีเพียง หนึ่ง constructor ตอไปนี้ที่
สามารถเปน constructor ในคลาส Beagle คือขอใด เพราะเหตุใด
A. Beagle() { }
B. Beagle() { super(); }
C. Beagle() { super("fido"); }
D. No constructor, allow the default constructor
2. จากคลาส Foo ที่กําหนดใหตอไปนี้
class Foo {
String doStuff(int x) { return "hello"; }
}
ขอใดตอไปนี้ไมสามารถเปน subclass ของ class Foo ได พรอมแสดงเหตุผลทุกขอ
A. String doStuff(int x) { return "hello"; }
B. int doStuff(int x) { return 42; }
C. public String doStuff(int x) { return "Hello"; }
D. protected String doStuff(int x) { return "Hello"; }
E. String doStuff(String s) { return "Hello"; }
F. int doStuff(String s) { return 42; }
3. แสดงผลลัพธที่ไดจากโปรแกรมนี้เมื่อไมเกิด error หรือหากเกิด error ใหแสดงเหตุผลที่เกิด
ขอผิดพลาดดังกลาวพรอมวิธีการแกปญหา
class ParentClass {
public int doStuff(int x) {
return x * 2;
}
}
25. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 205
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
public class ChildClass extends ParentClass {
public static void main(String [] args ) {
ChildClass cc = new ChildClass();
long x = cc.doStuff(7);
System.out.println("x = " + x);
}
public long doStuff(int x) {
return x * 3;
}
}
4. ผลลัพธที่ไดจากการรันโปรแกรมนี้คือ
class A {
public void baz() {
System.out.println("A");
}
}
public class B extends A {
public static void main(String [] args) {
A a = new B();
a.baz();
}
public void baz() {
System.out.println("B");
}
}
5. จากโปรแกรมที่ใหตอไปนี้ใหแสดงผลลัพธที่ไดจากการรัน
public class TestPoly {
public static void main(String [] args ){
Parent p = new Child();
}
}
26. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 206
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
class Parent {
public Parent() {
super();
System.out.println("instantiate a parent");
}
}
class Child extends Parent {
public Child() {
System.out.println("instantiate a child");
}
}
6. จากโปรแกรมตอไปนี้ผลลัพธที่ไดคือ
public class ThreeConst {
public static void main(String [] args) {
new ThreeConst(4L);
}
public ThreeConst(int x) {
this();
System.out.print(" " + (x * 2));
}
public ThreeConst(long x) {
this((int) x);
System.out.print(" " + x);
}
public ThreeConst() {
System.out.print("no-arg ");
}
}
27. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 207
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
7. จากโปรแกรมตอไปนี้ผลลัพธที่ไดคือ
class Super {
public Integer getLenght() { return new Integer(4); }
}
public class Sub extends Super {
public Long GetLenght() { return new Long(5); }
public static void main(String[] args) {
Super sooper = new Super();
Sub sub = new Sub();
System.out.println(sooper.getLenght().toString() + “,” + sub.getLenght().toString() );
}
}
8. ใหนิสิตทําการเขียนโปรแกรมดังตอไปนี้ แลวหาผลลัพธที่ไดพรอมอธิบายที่มาของผลลัพธดังกลาว
class Person {
protected String name;
public Person() { }
public Person(String n) { this.setName(n); }
public void setName(String n) { name = n; }
public String getName() { return name; }
public void printDetails() {
System.out.println("n** Details of a Person **");
System.out.println("name :: "+this.getName());
}
}
class Employee extends Person {
protected double salary;
public Employee(String n, double s) {
super(n);
this.setSalary(s);
}
28. บทที่ 8 การสืบทอดคุณสมบัติของคลาส หนาที่ 208
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
public void setSalary(double s) { salary = s; }
public String getSalary() {
Double d = new Double(salary);
String str = d.toString();
return str;
}
public void printDetails() {
super.printDetails();
System.out.println("salary :: "+this.getSalary());
}
}
class TestInheritance {
public static void main(String[] args) {
Person jenny = new Person("Jennifer");
Employee joey = new Employee("Joey", 200.0);
Employee judy = new Employee("Judith", 550.0);
System.out.println("*************************");
jenny.printDetails();
joey.printDetails();
judy.printDetails();
System.out.println("*************************");
}
}