Your SlideShare is downloading. ×
Inheritance
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Inheritance

7,856
views

Published on

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

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
7,856
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
222
Comments
0
Likes
5
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • s
  • s
  • s
  • s
  • s
  • Transcript

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