Bài giảng Phát triển phần mềm phân tán - Bài 8: Polymorphism and generic programming - Vũ Thị Hương Giang

LESSON VIII.  
Polymorphism and  
generic programming  
Vu Thi Huong Giang  
SoICT (School of Information and  
Communication Technology)  
HUST (Hanoi University of Science  
and Technology)  
Objectives  
• Master the polymorphism technique  
• Understand the Java generic programming  
Content  
• Polymorphism  
• Downcasting and upcasting  
• Overloading  
• Method call binding  
• Generic programming  
I. POLYMORPHISM  
1. Example  
Example  
• An operation that can be performed on a 2DShape object can  
also be performed on an object of one of three classes Triangle,  
Circle, Quadrilateral.  
– The super class 2DShape defines the common interface  
– The subclasses Triangle, Circle, Quadrilateral have to follow this  
interface (inheritance), but are also permitted to provide their own  
implementations (overriding)  
Once a method is requested  
through the 2DShape class  
reference, the objects 2DShape,  
Triangle, Circle and Quadrilateral  
respond differently  
Object  
Shape  
….  
2D Shape  
3D Shape  
Circle  
Triangle  
Quadrilateral  
Sphere  
Polyhedron  
Example  
public class 2DShape {  
public void display() {  
public class Point extends 2DShape {  
private int x, y;  
System.out.println("2D Shape");  
...  
}
public void display(){  
}
System.out.print("(" + x + "," + y + ")");  
}
}
public class Circle extends 2DShape{  
public static final double PI =  
public class Quadrilateral extends 2DShape {  
private Point p1, p2, p3, p4;  
3.14159;  
private Point p;  
private double r; //radious  
.....  
...  
public void display(){  
System.out.println("Quadrilateral: ");  
p1.display(); p2.display();  
p3.display(); p4.display();  
System.out.println();  
}
public void display(){  
System.out.print("Circle: " +  
r + ",");  
p.display();  
System.out.println();  
}
}
}
Example  
• There are many choice once a method is invoked  
through a super class reference.  
Object of super  
Object of Circle class  
class  
Circle: 1, (0,0)  
display()  
Object of Point class  
Application  
program  
2DShape  
display()  
(0,1)  
display()  
Object of Quadrilateral class  
Quadrilateral:  
(0,0), (0,1), (1,0), (1,1)  
display()  
2D Shape  
7
Polymorphism  
• Polymorphism means “many different forms” of  
objects  
– The ability of a reference variable to change behavior  
according to what object instance it is holding.  
• Objects of different subclasses are treated as  
objects of a single super class  
• Java choose the correct overridden method in the  
appropriate sub class associated with the object  
Exercise  
public class 2DShapeUsage {  
public static void main(String args[]) {  
Point point = new Point();  
Circle circle = new Circle();  
Quadrilateral quadri = new Quadrilateral();  
2DShape shape = new 2DShape();  
// display() method of 2DShape class is called  
shape.display(); // (1)  
// shape refers to the Point object; display() method of Point class is called  
shape = point; shape.display(); // (2)  
// display() method of Circle class is called  
shape = circle; shape.display(); // (3)  
// display() method of Quadrilateral class is called  
shape = quadri; shape.display(); // (4)  
2DShape shape2 = new Point();  
// display() method of Point class is called  
shape2.display(); // (5)  
}
}
II. DOWN CASTING AND  
UP CASTING  
Primitive type casting  
• Java performs automatic primitive type casting when:  
– Two types are compatible  
– The destination type is larger then the source type  
– Example:  
• int i;  
• double d = i;  
• We have to perform manual primitive type casting  
when:  
– Two types are compatible  
– The destination type is smaller then the source type  
– Example:  
• int i;  
• byte b = i; byte b = (byte)i;  
Reference type casting  
• Java performs automatic reference type casting  
when:  
– Two types are compatible  
– The destination type extends from the source type  
Up casting  
• We have to perform manual reference type  
casting when:  
– two types are compatible  
– The source type extends from the destination type  
Down casting  
Up casting  
• Substitute the reference of the sub class for the  
reference of the super class in the inheritance  
public class 2DShape {  
public void display() {  
// I  
...  
System.out.println("2D Shape");  
Point point = new Point();  
// 2 following statements are equivalent  
2DShape shape = (2DShape) point;  
2DShape shape = point;  
}
}
public class Point extends 2DShape {  
private int x, y;  
...  
public void displayPoint(){  
// II  
System.out.print(“Point”);  
...  
}
2DShape shape = new Point();  
}
Up casting  
• Java always remember what an object really is  
during up casting  
public class 2DShape {  
public interface 2DShape {  
public void display() {...}  
public void display();  
}
}
public class Point extends 2DShape {  
public class Point implements 2DShape {  
// interface’s methods  
public void display() {...}  
// class’ methods  
public void displayPoint(){...}  
}
public void displayPoint(){...}  
}
...  
...  
Point point = new Point();  
2DShape shape = point;  
shape.display(); //OK  
shape.displayPoint(); //impossible to call  
Point point = new Point();  
2DShape shape = point;  
shape.display(); //OK  
shape.displayPoint(); //impossible to call  
14  
Exercise: Implicit subclass object to  
super class object conversion  
• Refer to a super class object with a super class  
reference  
– Example ?  
• Refer to a sub class object with a subclass reference  
– Example ?  
• Refer to a sub class object with a super class  
reference is safe, but such code can only refer to  
super class members  
– Example ?  
• Refer to a super class object with a subclass reference  
is a syntax error  
– Example ?  
Downcasting  
• Substitute the reference of the super class for the  
reference of the sub class in the inheritance  
hierarchy.  
May not always suceed  
public class 2DShape {  
public void display() {  
...  
2DShape shape = new Point();  
Point point = (Point) shape; // 1  
point.displayPoint(); //possible to call  
System.out.println("2D Shape");  
}
}
public class Point extends 2DShape {  
...  
Point point = new Point();  
2DShape shape = point;  
if (shape instanceof 2DShape){  
Point tempObj= (Point)shape; // 2  
tempObj.displayPoint(); //possible to call  
}
private int x, y;  
...  
public void displayPoint(){  
System.out.print(“Point”);  
}
}
16  
Type Compatibility  
public class 2DShape {  
• Does it work ?  
public void display() {  
System.out.println("2D Shape");  
...  
}
Circle circle = new Circle();  
2DShape shape = circle;  
shape.changeWidth(20);  
}
Syntax error  
public class Circle extends 2DShape{  
public static final double PI = 3.14159;  
private Point p;  
• How can you make it  
work without changing  
the classes definitions?  
private double r; //radious  
...  
...  
public void changeRadious(double rad){  
Circle circle = new Circle();  
2DShape shape = circle;  
((Circle)shape).changeRadious(20); //ok  
r = rad;  
}
}
Problem with casting  
public class 2DShape {  
• Does it work ?  
public void display() {...}  
}
Point point = new Point();  
Circle circle = new Circle();  
TwoDimensionShape shape = circle;  
((Point)shape).displayPoint();  
public class Point extends 2DShape {  
private int x, y;  
...  
public void displayPoint(){...}  
}
Runtime exception  
public class Circle extends 2DShape{  
public static final double PI = 3.14159;  
private Point p;  
private double r; //radious  
...  
public void changeRadious(double rad){...}  
}
III. Method overloading  
• Overloading defines two or more methods with  
the same name in a class.  
• Overloaded methods are distinguished by their  
signature  
– same method name  
– different parameters or different number of parameters  
• Example:  
public class Point extends 2DShape {  
public void display(){  
System.out.print("(" + x + "," + y + ")");  
}
public void display(int x) {  
System.out.print("x-coordinate: " + x);  
}
}
19  
Example  
public class Point extends 2DShape {  
// 1  
public void display(){  
System.out.print("(" + x + "," + y + ")");  
}
// 2  
public void display(int x) {  
System.out.print("x-coordinate: " + x);  
}
// 3- syntax error: Creating overloaded methods 2, 3 with identical parameters lists  
public void display(int y) {  
System.out.print(“y-coordinate: " + y);  
}
// 4- syntax error: Methods 1, 4 can not be distinguished by return type  
public boolean display() {  
System.out.print("x-coordinate: " + x);  
return true;  
}
}
Tải về để xem bản đầy đủ
pdf 44 trang Thùy Anh 21300 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Phát triển phần mềm phân tán - Bài 8: Polymorphism and generic programming - Vũ Thị Hương Giang", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_phat_trien_phan_mem_phan_tan_bai_8_polymorphism_an.pdf