PEWARISAN D E W I S A R T I K A, M. K O M
PENDAHULUAN Pewarisan (inheritance) merupakan suatu hubungan antara dua buah kelas atau lebih, dimana ada kelas yang memiliki atribut dan method yang sama dengan kelas lainnya beserta atribut dan method tambahan yang merupakan sifat khusus dari kelas turunannya. Contoh : Kelas titik memiliki kelas turunan titik3d
KEYWORD Keyword yang digunakan untuk menyatakan bahwa kelas tsb merupakan kelas turunan adalah extends Bentuk Umum : <modifier> class <kelas turunan> extends <kelas induk>
CONTOH X ----- ----- Kelas Induk public class X... Y ----- ----- Kelas Turunan public class Y extends X...
MODIFIER PADA PEWARISAN public class X private int a; protected int b; public int c; private SetA(int a)... protected SetB(int b)... public SetC(int C)... public class Y extends X protected int b; public int c; protected SetB(int b) public SetC(int C)
MODIFIER PADA PEWARISAN public class X private int a; protected int b; public int c; private SetA(int a)... protected SetB(int b)... public SetC(int C)... public class Y extends X private int d; protected int e; public int GetD()... protected int b; public int c; protected SetB(int b) public SetC(int C) Public class Z extends Y protected int e; public int GetD()...
MODIFIER PADA PEWARISAN public class X private int a; protected int b; public int c; private SetA(int a)... protected SetB(int b)... public SetC(int C)... public class Y extends X private int d; protected int e; public int GetD()... protected int b; public int c; protected SetB(int b) public SetC(int C) Public class Z extends X protected int b; public int c; protected SetB(int b) public SetC(int C)
KONSTRUKTOR DAN DESTRUKTOR PADA PEWARISAN Kelas induk dan kelas anak sama-sama dapat memiliki konstruktor dan destruktor Konstruktor kelas induk akan lebih dulu dijalankan, sebelum konstruktor kelas anak Destruktor kelas anak akan lebih dulu dijalankan, sebelum destruktor kelas induk
public class titik private int x; private int y; titik() x = 0; y = 0; public void SetX(int x) this.x = x; public void SetY(int y) this.y = y; public int GetX() return x; public int GetY() return y; public class titik3d extends titik private int x; titik3d() z = 0; public void SetZ(int z) this.z = z; public int GetY() return y; CONTOH PROGRAM
CONTOH PROGRAM public class main public static void main(string[] args) titik objtitik = new titik(); System.out.println( Titik Awal = ( +objtitik.getx()+, +objtitik.gety()+ ) ); objtitik.setx(2); objtitik.sety(4); System.out.println( Titik Baru = ( +objtitik.getx()+, +objtitik.gety()+ ) ); titik3d obj3d = new titik3d(); System.out.println( Titik Awal = ( +obj3d.getx()+, +obj3d.gety()+, +obj3d.getz()+ ) ); obj3d.setz(2); System.out.println( Titik Baru = ( +obj3d.getx()+, +obj3d.gety()+, +obj3d.getz()+ ) );
SUPER Keyword super digunakan untuk memanggil konstruktor kelas super terdekat Jika dalam sebuah konstruktor telah dituliskan keyword super, maka tidak diperbolehkan adanya penulisan keyword this
CONTOH PROGRAM public class x public x() System.out.println( konstruktor x ); public class y extends x public Y() super(); System.out.println( konstruktor y );
CONTOH PROGRAM public class x public x() System.out.println( konstruktor x ); public class y extends x public Y() System.out.println( konstruktor y ); public class z extends y super(); System.out.println( konstruktor z );
OVERRIDDING METHOD Kelas anak dapat mengabaikan method yang telah didefinisikan pada kelas induk, dengan cara mengimplementasikan method baru dengan nama yang sama pada kelasnya.
CONTOH PROGRAM public class X public void print() System.out.println( ini method kelas X ); public class Y public void print() System.out.println( ini method overide kelas Y );
SELESAI T E R U S B E R L A T I H!!