Polymorphism - Overiding & Overloading [Praktek]
-
Upload
lim-cia-chien -
Category
Documents
-
view
37 -
download
0
Transcript of Polymorphism - Overiding & Overloading [Praktek]
![Page 1: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/1.jpg)
![Page 2: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/2.jpg)
• Overriding• Overloading• Constructor overloading• Polymorphism• Virtual Method Invocation• Polymorphic arguments• Operator instanceof• Casting & Convertion Objects
2
![Page 3: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/3.jpg)
Latihan 1Overloading, Overriding dan Polymorphism
![Page 4: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/4.jpg)
Lat 1. Virtual Method Invocation dan Polymorphic Arguments Simpan dengan nama Employee.java
![Page 5: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/5.jpg)
![Page 6: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/6.jpg)
![Page 7: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/7.jpg)
![Page 8: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/8.jpg)
Lat 2. Polymorphism: Method overloading
MyRect.java
import java.awt.Point;class MyRect { int x1 = 0; int y1 = 0; int x2 = 0; int y2 = 0;
MyRect buildRect(int x1, int y1, int x2, int y2) { this.x1 = x1; this.y1 = y1; this.x2 = x2; this.y2 = y2; return this; }
![Page 9: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/9.jpg)
MyRect buildRect(Point topLeft, Point bottomRight) { x1 = topLeft.x; y1 = topLeft.y; x2 = bottomRight.x; y2 = bottomRight.y; return this; }
MyRect buildRect(Point topLeft, int w, int h) { x1 = topLeft.x; y1 = topLeft.y; x2 = (x1 + w); y2 = (y1 + h); return this; }
void printRect(){ System.out.print("MyRect: <" + x1 + ", " + y1); System.out.println(", " + x2 + ", " + y2 + ">"); }
![Page 10: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/10.jpg)
public static void main(String[] arguments) { MyRect rect = new MyRect(); System.out.println("Calling buildRect with coordinates 25,25, 50,50:"); rect.buildRect(25, 25, 50, 50); rect.printRect(); System.out.println("***"); System.out.println("Calling buildRect with points (10,10), (20,20):"); rect.buildRect(new Point(10,10), new Point(20,20)); rect.printRect(); System.out.println("***"); System.out.print("Calling buildRect with 1 point (10,10),"); System.out.println(" width (50) and height (50):"); rect.buildRect(new Point(10,10), 50, 50); rect.printRect(); System.out.println("***"); }}
![Page 11: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/11.jpg)
Latihan 2Casting & Convertion Objects
![Page 12: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/12.jpg)
Latihan 1: Object Reference Conversion
Buat dalam file bernama Fruit.java
![Page 13: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/13.jpg)
Latihan 2: Object Reference Conversion
![Page 14: Polymorphism - Overiding & Overloading [Praktek]](https://reader035.fdocuments.us/reader035/viewer/2022062407/55cf9b62550346d033a5dda0/html5/thumbnails/14.jpg)
Latihan 3: Object Reference Conversion, Method Call, dan Casting
Dengan cara yang sama :• Cobalah contoh-contoh program yang ada
pada ppt teori tentang Conversion• Cobalah contoh-contoh program yang ada
pada ppt teori tentang Method Call • Cobalah contoh-contoh program yang ada
pada ppt teori tentang Casting