Polymorphism Praktikum 9 Pemrograman Berbasis Obyek Oleh Tita
Polymorphism Praktikum 9 Pemrograman Berbasis Obyek Oleh Tita Karlita
Topik l l l l Overriding Overloading Constructor overloading Polymorphism Virtual Method Invocation Polymorphic arguments Operator instanceof Casting & Convertion Objects
Latihan 1 Overloading, Overriding dan Polymorphism
Lat 1. Virtual Method Invocation dan Polymorphic Arguments Simpan dengan nama Employee. java
Lat 2. Polymorphism: Method overloading My. Rect. java import java. awt. Point; class My. Rect { int x 1 = 0; int y 1 = 0; int x 2 = 0; int y 2 = 0; My. Rect build. Rect(int x 1, int y 1, int x 2, int y 2) { this. x 1 = x 1; this. y 1 = y 1; this. x 2 = x 2; this. y 2 = y 2; return this; }
My. Rect build. Rect(Point top. Left, Point bottom. Right) { x 1 = top. Left. x; y 1 = top. Left. y; x 2 = bottom. Right. x; y 2 = bottom. Right. y; return this; } My. Rect build. Rect(Point top. Left, int w, int h) { x 1 = top. Left. x; y 1 = top. Left. y; x 2 = (x 1 + w); y 2 = (y 1 + h); return this; } void print. Rect(){ System. out. print("My. Rect: <" + x 1 + ", " + y 1); System. out. println(", " + x 2 + ", " + y 2 + ">"); }
public static void main(String[] arguments) { My. Rect rect = new My. Rect(); System. out. println("Calling build. Rect with coordinates 25, 50, 50: "); rect. build. Rect(25, 50, 50); rect. print. Rect(); System. out. println("***"); System. out. println("Calling build. Rect with points (10, 10), (20, 20): "); rect. build. Rect(new Point(10, 10), new Point(20, 20)); rect. print. Rect(); System. out. println("***"); System. out. print("Calling build. Rect with 1 point (10, 10), "); System. out. println(" width (50) and height (50): "); rect. build. Rect(new Point(10, 10), 50); rect. print. Rect(); System. out. println("***"); } }
Latihan 2 Casting & Convertion Objects
Latihan 1: Object Reference Conversion Buat dalam file bernama Fruit. java
Latihan 2: Object Reference Conversion
Latihan 3: Object Reference Conversion, Method Call, dan Casting Dengan cara yang sama seperti latihan 9: l Cobalah contoh-contoh program yang ada ppt teori tentang Conversion l Cobalah contoh-contoh program yang ada ppt teori tentang Method Call l Cobalah contoh-contoh program yang ada ppt teori tentang Casting
- Slides: 14