PemrogramanBerorientasiObjek<br />Inheritance (Pewarisan)<br />
Pengertian Inheritance <br />Prosespewarisan data dan method darisuatukelaskepadakelas yang lain<br />Class ygmewariskanSu...
Keyword Extends<br />bentukumumpada Java :<br />	<hakakses> class <subclass> extends <superclass><br />Contoh :<br />	publ...
Keyword Super<br />untukmemanggilkonstruktordan method oleh subclass yang dimilikiolehsuperclass<br /><ul><li>Bentukumumun...
Jenis-Jenis Inheritance <br />Single Inheritance	(Pewarisan Tunggal)<br />Multiple Inheritance (PewarisanJamak)<br />Virtu...
Single Inheritance (Pewarisan Tunggal)<br />Adalahpewarisan yang jumlahkelasdasarnya (basic class / superclass) tunggal, t...
Single Inheritance (Pewarisan Tunggal)<br />
Syntax Single Inheritance<br />C++<br />JAVA<br />class A<br />{<br />  . . .<br />};<br />class B : public A<br />{<br />...
Contoh Single Inheritance ( Java )<br />public class Komputer {<br />    String Processor;<br />int Monitor;<br />intHardD...
Contoh Single Inheritance ( Java )<br />public class Laptop extends Komputer{<br />       String typeBattery;<br />intdura...
Contoh Single Inheritance ( Java )<br />public class Circle { <br />  private double radius; <br />  Circle(double r) {   ...
Contoh Single Inheritance ( Java )<br />public class Cylinder extends Circle { <br />  private double length; <br />  publ...
Contoh Single Inheritance ( Java )<br />public class Test { <br />   public static void main(String [] Args) { <br />     ...
Multiple Inheritance (PewarisanJamak)<br />Kelas yang diturunkanlebihdarisatukelas yang berbeda (jumlahkelasdasarnyalebihd...
Multiple Inheritance (PewarisanJamak)<br />
Syntax Multiple Inheritance<br />class A<br />{<br />  . . .<br />};<br />class B<br />{<br />  . . .<br />}<br />class C:...
Contoh Multiple Inheritance (C++) <br />class ayah{<br />      public:<br />             void rambut(){<br />cout<<">Rambu...
Contoh Multiple Inheritance (C++) <br />class anak : public ayah, public ibu{<br />      public:<br />             void hi...
Virtual Multiple Inheritance<br />Kelas yang diturunkanlebihdarisatukelas yang berbeda (jumlahkelasdasarnyalebihdarisatu)<...
Virtual Multiple Inheritance<br />
Syntax VirtualMultiple Inheritance<br />class A<br />{<br />  . . .<br />};<br />class B: virtual public A<br />{<br />  ....
Contoh Virtual Multiple Inheritance<br />class hewan<br />{<br />public:<br />void bergerak()<br />{<br />cout<<" # Berger...
Hal ygperludipertimbangkansaatmewariskan method :<br />Jika method disuperclass PUBLIC makasemua subclass bersifat PUBLIC<...
HakAkses Subclass danSuperclass<br />
Selesai<br />UniversitasKomputer Indonesia<br />
Upcoming SlideShare
Loading in...5
×

Inheritance

8,076

Published on

Jenis-Jenis Inheritance Dan Hak Akses Super Class dan Sub Class

Published in: Education, Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
8,076
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
230
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide
  • s
  • s
  • s
  • s
  • s
  • Inheritance

    1. 1. PemrogramanBerorientasiObjek<br />Inheritance (Pewarisan)<br />
    2. 2. Pengertian Inheritance <br />Prosespewarisan data dan method darisuatukelaskepadakelas yang lain<br />Class ygmewariskanSuperclass<br />Class ygdiwariskan Subclass<br />
    3. 3. Keyword Extends<br />bentukumumpada Java :<br /> <hakakses> class <subclass> extends <superclass><br />Contoh :<br /> public class mobilextendsKendaraanBermotor<br />bentukumumpada C++ :<br /> class <subclass> : <hakakses> <superclass><br />contoh :<br /> class mobil : public kendaraan_bermotor<br />
    4. 4. Keyword Super<br />untukmemanggilkonstruktordan method oleh subclass yang dimilikiolehsuperclass<br /><ul><li>Bentukumumuntukmemanggilkonstruktor :</li></ul>super ()<br />super(parameter)<br /><ul><li>Bentukumumuntukmemanggil method :</li></ul>super.method(parameter)<br />
    5. 5. Jenis-Jenis Inheritance <br />Single Inheritance (Pewarisan Tunggal)<br />Multiple Inheritance (PewarisanJamak)<br />Virtual Multiple Inheritance (PewarisanJamak Maya)<br />
    6. 6. Single Inheritance (Pewarisan Tunggal)<br />Adalahpewarisan yang jumlahkelasdasarnya (basic class / superclass) tunggal, tapikelasturunan (subclass) bisalebihdarisatu<br />Konsep inheritance yang ada di Java hanya memperkenankan adanya single inheritance.<br />
    7. 7. Single Inheritance (Pewarisan Tunggal)<br />
    8. 8. Syntax Single Inheritance<br />C++<br />JAVA<br />class A<br />{<br /> . . .<br />};<br />class B : public A<br />{<br /> . . .<br />}<br />class A<br />{<br /> . . .<br />};<br />Public class B extends A<br />{<br /> . . .<br />}<br />
    9. 9. Contoh Single Inheritance ( Java )<br />public class Komputer {<br /> String Processor;<br />int Monitor;<br />intHardDisk;<br />int Memory;<br /> public intgetHardDisk() {<br /> return HardDisk;<br /> }<br /> public void setHardDisk(intHardDisk) {<br />this.HardDisk = HardDisk;<br /> }<br /> public intgetMemory() {<br /> return Memory;<br /> }<br /> public void setMemory(int Memory) {<br />this.Memory = Memory;<br /> }<br /> public intgetMonitor() {<br /> return Monitor;<br /> }<br /> public void setMonitor(int Monitor) {<br />this.Monitor = Monitor;<br /> }<br /> public String getProcessor() {<br /> return Processor;<br /> }<br /> public void setProcessor(String Processor) {<br />this.Processor = Processor;<br /> }<br />}<br />
    10. 10. Contoh Single Inheritance ( Java )<br />public class Laptop extends Komputer{<br /> String typeBattery;<br />intdurasiBattery;<br /> public intgetDurasiBattery() {<br /> return durasiBattery;<br /> }<br /> public void setDurasiBattery(intdurasiBattery) {<br />this.durasiBattery = durasiBattery;<br /> }<br /> public String getTypeBattery() {<br /> return typeBattery;<br /> }<br /> public void setTypeBattery(String typeBattery) {<br />this.typeBattery = typeBattery;<br /> }<br />}<br />
    11. 11. Contoh Single Inheritance ( Java )<br />public class Circle { <br /> private double radius; <br /> Circle(double r) { //Constructor <br /> radius = r; <br /> } <br /> Circle() { //Constructor <br /> radius = 1.0; <br /> } <br /> public double getRadius() { <br /> return radius; <br /> } <br /> public double findArea() { <br /> return radius*radius*3.14;<br /> } <br />} <br />
    12. 12. Contoh Single Inheritance ( Java )<br />public class Cylinder extends Circle { <br /> private double length; <br /> public Cylinder() { //Constructor <br />super(); <br /> length = 1.0; <br /> } <br /> public Cylinder(double r,double l) { //Constructor <br />super(r); <br /> length = l; <br /> } <br /> public double getLength() { <br /> return length; <br /> } <br /> public double findArea() { <br />System.out.println(“Call Cylinder find Area”); <br /> return 2*super.findArea()+2*getRadius()*3.14159*length; <br />}<br /> public double findVolume() { <br /> return findArea() * length; <br /> } <br />} <br />
    13. 13. Contoh Single Inheritance ( Java )<br />public class Test { <br /> public static void main(String [] Args) { <br /> //Create Cylinder object and display it's properties <br /> Cylinder myCylinder = new Cylinder(5.0,2.0); <br />System.out.println("The length : "+myCylinder.getLength()); <br />System.out.println("The radius : "+myCylinder.getRadius()); <br />System.out.println("The Cylinder Volume : "+myCylinder.findVolume()); <br />System.out.println("The area of Circle : "+myCylinder.findArea()); <br /> } <br />} <br />
    14. 14. Multiple Inheritance (PewarisanJamak)<br />Kelas yang diturunkanlebihdarisatukelas yang berbeda (jumlahkelasdasarnyalebihdarisatu)<br />Java tidakmendukung Multiple Inheritance<br />C++ mendukungMultiple Inheritance<br />Java menggunakan interface untukmenggantikan konsep multiple inheritance <br /> Multiple Interface Inheritance<br />
    15. 15. Multiple Inheritance (PewarisanJamak)<br />
    16. 16. Syntax Multiple Inheritance<br />class A<br />{<br /> . . .<br />};<br />class B<br />{<br /> . . .<br />}<br />class C: public A, public B<br />{<br /> . . .<br />}<br />
    17. 17. Contoh Multiple Inheritance (C++) <br />class ayah{<br /> public:<br /> void rambut(){<br />cout<<">RambutnyaKeriting"<<endl;<br /> } <br />};<br />class ibu{<br /> public:<br /> void kulit(){<br />cout<<">KulitnyaPutih"<<endl;<br /> }<br /> };<br />
    18. 18. Contoh Multiple Inheritance (C++) <br />class anak : public ayah, public ibu{<br /> public:<br /> void hidung(){<br />cout<<">Hidunganakitumancung"<<endl<<endl;<br /> } <br /> };<br />class cucu : public ayah, public ibu{<br /> public:<br /> void pandai(){<br />cout<<">CucudariKakekdanNenekitupandaiberhitung"<<endl<<endl;<br /> } <br />};<br />
    19. 19. Virtual Multiple Inheritance<br />Kelas yang diturunkanlebihdarisatukelas yang berbeda (jumlahkelasdasarnyalebihdarisatu)<br />Hanyasaja, beberapadiantarakelasdasartersebutmerupakankelasturunandarikelasdasar yang sama<br />
    20. 20. Virtual Multiple Inheritance<br />
    21. 21. Syntax VirtualMultiple Inheritance<br />class A<br />{<br /> . . .<br />};<br />class B: virtual public A<br />{<br /> . . .<br />};<br />class C: virtual public A<br />{<br /> . . .<br />};<br />class D: public B, public C<br />{<br /> . . .<br />};<br />
    22. 22. Contoh Virtual Multiple Inheritance<br />class hewan<br />{<br />public:<br />void bergerak()<br />{<br />cout<<" # Bergerakberpindahtempat"<<endl;<br />}<br />};<br />class kuda: virtual public hewan<br />{<br />public :<br />void berlari()<br />{<br />cout<<" # Berlarinyasangatcepat"<<endl;<br />}<br />};<br />class burung: virtual public hewan<br />{<br />public:<br />void terbang()<br />{<br />cout<<" # Terbangmenembusawan"<<endl;<br />}<br />};<br />class pegasus: public kuda, public burung<br />{<br />public:<br />void lariterbang()<br />{<br />cout<<"# Bersayap, laridandapatterbangkeangkasa"<<endl;<br />}<br />};<br />
    23. 23. Hal ygperludipertimbangkansaatmewariskan method :<br />Jika method disuperclass PUBLIC makasemua subclass bersifat PUBLIC<br />Jika method disuperclass PROTECTED makabisamenjadi PROTECTED atau PUBLIC untuksemua subclass<br />Jika method disuperclass DEFAULT makapada subclass bisamenjadi PRIVATE<br />Jika method disuperclass PRIVATE, makatidakbisadiwariskan<br />HubunganantaraAksesdan Inheritance<br />
    24. 24. HakAkses Subclass danSuperclass<br />
    25. 25. Selesai<br />UniversitasKomputer Indonesia<br />
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×