Pemrograman Berorientasi Object Generic Type Inheritance Abas Setiawan
Pemrograman Berorientasi Object Generic Type & Inheritance Abas Setiawan Teknik Informatika s 1
Generic Type
Generic Type • Generic Type/ Tipe Generik merupakan class atau interface generik yang memperbolehkan semua jenis tipe data sebagai suatu parameter. • Sebelumnya sudah di implementasi pada Array. List dan Vector.
Perbandingan Non Generic public class Box { Generic public class Box<T> { private int object; private T t; public void set(int object) { this. object = object; public void set(T t) { } } public int get() { public T get() { this. t = t; return object; return t; } }
Versi Generik dari “Box” Class • Pada versi non-generic “Box Class” jika seandainya instance dari Box adalah Kotak dan Kotak. set(“ 50”), maka akan menghasilkan error. • Error disebabkan hanya boleh diset oleh nilai integer saja. • Pada versi generic dapat di set oleh berbagai tipe data, baik primitive maupun tipe data bentukan, dan bahkan array/list. • Format class generic: • class name<T 1, T 2, . . . , Tn> { /*. . . */ } • Instance: • Box<Integer> integer. Box = new Box<Integer>();
Penamaan Parameter pada tipe generik • • • E - Element (used extensively by the Java Collections Framework) K - Key N - Number T - Type V - Value S, U, V etc. - 2 nd, 3 rd, 4 th types
public class Ordered. Pair<K, V> { private K key; private V value; Multiple type parameter public Ordered. Pair(K key, V value) { this. key = key; this. value = value; } public K get. Key() { return key; } public V get. Value() { return value; } } Ordered. Pair<String, String> p 2 = new Ordered. Pair<String, String>(“ 1", “ 2"); System. out. println(p 2. get. Value());
Generic static method public static < E > void print. Array( E[] input. Array ) { // Display array elements for(E element : input. Array) { System. out. printf(element+” ”); } System. out. println(); }
Konsep Pewarisan/Inheritance
Konsep Pewarisan • OOP memperbolehkan kita untuk menurunkan class baru dari class yang sudah ada sebelumnya. • Dapat digunakan untuk mencegah redudansi class, membuat sistem lebih mudah dipahami, dan membuat sistem lebih mudah di maintain. • Dapat memodelkan objek dengan tipe class yang sama. • General to special class • Special class mewarisi sifat property dan method dari general class
Contoh Class Diagram Super. Class Geometric. Object Circle Rectangle Sub. Class
Keyword “extends”
Contoh implementasi public class Animal{ } public class Mammal extends Animal{ } public class Reptile extends Animal{ } public class Dog extends Mammal{ }
Jenis-Jenis Inheritance
“Super” Keyword • Keyword “super” digunakan untuk merujuk langsung variable/method/konstruktor dari super class. • Sintak: • • • super(); Atau super(parameter list);
class Animal{ String color="white"; } class Dog extends Animal{ String color="black"; void print. Color(){ System. out. println(color); System. out. println(super. color); } } class Test. Super 1{ public static void main(String args[]){ Dog d=new Dog(); d. print. Color(); } } Super untuk merujuk variable pada super class
class Animal{ void eat(){System. out. println("eating. . . "); } } class Dog extends Animal{ void eat(){System. out. println("eating bread. . . "); } void bark(){System. out. println("barking. . . "); } void work(){ super. eat(); bark(); } } class Test. Super 2{ public static void main(String args[]){ Dog d=new Dog(); d. work(); }} Super untuk merujuk method pada super class
class Animal{ Animal(){System. out. println("animal is created"); } } class Dog extends Animal{ Dog(){ super(); System. out. println("dog is created"); } } class Test. Super 3{ public static void main(String args[]){ Dog d=new Dog(); } } Super untuk merujuk konstruktor pada super class
Referensi • Introduction to Programming, Binus University • Introduction to Java Programming, Comprehensive Version (10 th Edition) by Y. Daniel Liang, 2015, Pearson Education, Inc. • https: //docs. oracle. com/javase/tutorial/java/generics/types. html • http: //www. javatpoint. com/super-keyword
- Slides: 19