MODUL 7 PBO 1 PEWARISAN INHERITANCE Pewarisan Pewarisan
MODUL 7 PBO 1 PEWARISAN (INHERITANCE)
Pewarisan • Pewarisan merupakan sebuah bentuk “penggunaan kembali” (reusability); dimana class baru dibuat dari class yang pernah ada yang (biasanya) ditambah fasilitasnya. • Setiap class turunan dapat menjadi class pokok (induk) untuk class turunan yang akan datang. • Dalam pewarisan, constructor tidak diwariskan pada class turunannya, kecuali jika digunakan perintah super.
Pewarisan • Pewarisan tunggal (single inheritance) merupakan pewarisan dari satu class pokok (induk). • Pewarisan ganda (multiple inheritance) merupakan pewarisan dari dua atau lebih class pokok. • Java tidak mendukung multiple inheritance. .
Pewarisan • Di JAVA, class pokok disebut dengan superclass, sedangkan class turunannya disebut dengan subclass. • Misalnya: class bawah merupakan subclass dari class atas, di JAVA dituliskan sbb: class bawah extends atas { … }
Pewarisan class atas class bawah
Class atas : class atas { atas() {} //constructor void cetak 1() { System. out. println("Ada di class ATAS. . "); } }
Class bawah : class bawah extends atas { void cetak 2() { System. out. println("Ada di class BAWAH. . "); } public static void main(String[] args) { bawah bwh = new bawah(); bwh. cetak 1(); bwh. cetak 2(); } }
Kendali akses (lingkup class) • Private. Feature yang “visible” hanya untuk class dimana feature tersebut didefinisikan. • Protected. Feature yang disembunyikan terhadap class client, tetapi “visible” untuk class turunan. • Public. Feature yang “visible”, dapat dipakai oleh semua class yang ada (client, turunan/subclass).
Class atas_priv : class atas_priv { private int i = 5; atas_priv() {} //constructor void cetak 1() { System. out. println("Ada di class ATAS. . "); System. out. println("i = "+i); } }
Class bawah_priv : class bawah_priv extends atas_priv { void cetak 2() { System. out. println("Ada di class BAWAH. . "); System. out. println("i = "+i); } public static void main(String[] args) { bawah_priv bwh = new bawah_priv(); bwh. cetak 1(); bwh. cetak 2(); } }
• Apa yang terjadi dengan program class bawah_priv di atas ?
Class atas_pub : class atas_pub { public int i = 5; atas_pub() {} //constructor void cetak 1() { System. out. println("Ada di class ATAS. . "); System. out. println("i = "+i); } }
Class bawah_pub : class bawah_pub extends atas_pub { void cetak 2() { System. out. println("Ada di class BAWAH. . "); System. out. println("i = "+i); } public static void main(String[] args) { bawah_pub bwh = new bawah_pub(); bwh. cetak 1(); bwh. cetak 2(); } }
• Apa yang terjadi dengan program class bawah_pub di atas ?
Overriding • Overiding merupakan pendefinisian suatu method dengan nama beserta signature yang sama dengan nama beserta signature di superclass-nya. • Signature adalah daftar parameter. • Pada contoh class bawah_pub: dapat ditambahkan method yang meng-override method cetak 1().
Class bawah_pub 1 : class bawah_pub 1 extends atas_pub { void cetak 1() { System. out. println("Ada di class BAWAH. . "); } void cetak 2() { System. out. println("Ada di class BAWAH. . "); System. out. println("i = "+i); } public static void main(String[] args) { bawah_pub 1 bwh = new bawah_pub 1(); bwh. cetak 2(); } }
• Apa yang terjadi dengan program class bawah_pub 1 di atas ?
Overriding • Apa yang akan terjadi jika method cetak 1 pada class atas_pub dimodifikasi menjadi: class atas_pub { public int i = 5; atas_pub() {} //constructor public void cetak 1() { System. out. println("Ada di class ATAS. . "); System. out. println("i = "+i); } }
Overriding • Sedangkan pada class bawah_pub method cetak 1 dioverride menjadi: class bawah_pub 1 extends atas_pub { private void cetak 1() { System. out. println("Ada di class BAWAH. . "); } void cetak 2() { System. out. println("Ada di class BAWAH. . "); System. out. println("i = "+i); } public static void main(String[] args) { bawah_pub 1 bwh = new bawah_pub 1(); bwh. cetak 2(); } }
Overriding • Akan terjadi error karena method pada class turunan harus lebih luas atau sama sifat kendali aksesnya degan method pada class pokok.
Perintah super • Method pada subclass dapat memanggil method pada superclass-nya dengan menggunakan perintah super. Contoh: class bawah_pub 1 extends atas_pub { void cetak 1() { super. cetak 1(); System. out. println(“Telah selesai memanggil cetak 1 milik superclass. . "); } void cetak 2() { System. out. println("Ada di class BAWAH. . "); System. out. println("i = "+i); } public static void main(String[] args) { bawah_pub 1 bwh = new bawah_pub 1(); bwh. cetak 2(); } } • Perintah super hanya bisa memanggil method yang berada pada class satu tingkat di atasnya (induk langsungnya)
Perintah super • Perintah super dapat pula digunakan untuk memanggil constructor dari superclassnya. Contoh : class hewan { static int kaki, umur; static String gigi; hewan(int k) { kaki = k; } hewan(int k, String g) { kaki = k; gigi = g; } }
Perintah super class karnivora extends hewan { karnivora(int k) { super(k); } karnivora(int k, String g) { super(k, g); } }
Latihan – Buatlah class Karyawan Tetap dan class Karyawan Kontrak yang merupakan turunan dari class Karyawan yang pernah Anda buat sebelumnya. Setiap karyawan tetap mempunyai atribut gaji pokok, dan method untuk menghitung total gaji yang merupakan penjumlahan gaji pokok dengan tunjangan anak. Sedangkan karyawan kontrak mempunyai atribut upah harian dan method total upah yang merupakan penjumlahan upah harian dikalikan jumlah hari masuk, ditambah tunjangan anak. Manfaatkan tipe akses yang sesuai, overriding dan perintah super jika diperlukan
- Slides: 24