Memanggil Class dalam Pewarisan di Java

Untuk lebih memahami konsep inheritance (pewarisan) pada OOP di Java, kita pelajari dulu koding di bawah ini:

class Meal {
      Meal() {
            System.out.println(”Meal()”);
     }
}
class Bread {
       Bread() {
            System.out.println(”Bread()”);
      }
}
class Cheese {
       Cheese() {
             System.out.println(”Cheese()”);
      }
}
class Lettuce {
        Lettuce() {
             System.out.println(”Lettuce()”);
     }
}
class Lunch extends Meal {
        Lunch() {
             System.out.println(”Lunch()”);
    }
}
class PortableLunch extends Lunch {
        PortableLunch() {
             System.out.println(”PortableLunch()”);
    }
}
class Sandwich extends PortableLunch {
            private Bread b = new Bread();
            private Cheese c = new Cheese();
            private Lettuce l = new Lettuce();
public Sandwich() {
            System.out.println(”Sandwich()”);
   }
}
public class MainClass {
       public static void main(String[] args) {
              new Sandwich();
     }
}

Hasilnya:

Lunch()
PortableLunch()
Bread()
Cheese()
Lettuce()
Sandwich()
About

Seseorang yang terus berusaha menjadi seorang programmer dan blogger sejati di jagad Raya ini.

Posted in Java Programming
8 comments on “Memanggil Class dalam Pewarisan di Java
  1. Endy says:

    Aduh mas, apa ini? saya ndak ngerti..

  2. eko zulkaryanto says:

    o ya, sorry bro, judulnya salah, bukan “constructor” tapi “class”……
    ga ngerti gimana, dicoba, ditrace aja satu-satu…

  3. 4n1 says:

    Maaf, aku coba bikin program kamu dan ini nih hasilnya:

    Meal()
    Lunch()
    PortableLunch()
    Bread()
    Cheese()
    Lettuce()
    Sandwich()

    Kok agak beda sama hasil kamu ya?

  4. deta says:

    mas, tolong dong, gimana cara nge-Trace java. thank u

  5. mpith says:

    punten…..
    bingung ne,, ama pewarisan di java????

    tpi mke switch-case???? gmana y????

  6. ecko says:

    coba deh ke java2s.com banyak tutorial java-nya…
    semoga membantu nih ada link yg pake switch case:
    http://www.java2s.com/Code/Java/Data-Type/switchwithcharvalue.htm

    dan type-nya klo ga salah harus integer.

    pewarisan memang agak susah di pahami.

    tp ya asal ada keyword “extends” ini pasti anaknya.
    sifat orang tua di turunkan ke anak.
    berarti semua properti yg ada di orang tua diturunkan ke anak.

  7. Terrific post however I was wondering if you could write a litte more on this topic?
    I’d be very thankful if you could elaborate a little bit further.
    Many thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Community

INDC

MUGI

WSS-ID

SQLServer-Indo

Indonesia Student Portal

Pengunjung
  • 120,657 orang
Yg lg OL di sini
site statistics
%d bloggers like this: