Laman

Minggu, 06 Mei 2012

Hirarki Class Hewan


HewanApp.java
public class HewanApp {
    public static void main(String[] args) {

        //bikin objek dulu

        //Superclass//
        Hewan superH = new Hewan();

        //sub hewan//
        HewanMamalia subMa = new HewanMamalia();
        HewanUnggas subUng = new HewanUnggas();
        HewanReptil subRep = new HewanReptil();


        //submamalia//
        MamaliaHerbivora subMaHer = new MamaliaHerbivora();
        MamaliaKarnivora subMaKar = new MamaliaKarnivora();
        MamaliaOmnivora subMaOmni = new MamaliaOmnivora();
     

        System.out.println("SuperClass");
        superH.TampilkanHewan();
        System.out.println("");

        System.out.println("SubClass Hewan");
        subMa.TampilkanHewanMamalia();
        subUng.TampilkanHewanUnggas();
        subRep.TampilkanHewanReptil();
        System.out.println("");

        System.out.println("SubClass Hewan Mamalia");
        subMaHer.TampilkanMamaliaHerbivora();
        subMaKar.TampilkanMamaliaKarnivora();
        subMaOmni.TampilkanMamaliaOmnivora();
        System.out.println("");

     
    }
}

Hewan.Java

public class Hewan {
    protected String x="salah satu makhluk hidup ";
    void TampilkanHewan() {
        System.out.println("Hewan adalah " + x);
    }
}

class HewanMamalia extends Hewan {
    protected String mamalia="yang menyusui anaknya ";
    void TampilkanHewanMamalia() {
        System.out.println("Hewan Mamalia : " + (x+mamalia));
    }
}

class HewanUnggas extends Hewan {
    protected String unggas="yang Bertelur ";
    void TampilkanHewanUnggas() {
        System.out.println("Hewan unggas : " + (x+unggas));
    }
}

class HewanReptil extends Hewan {
    protected String reptil="yang Berdarah Dingin ";
    void TampilkanHewanReptil() {
        System.out.println("Hewan reptil : " + (x+reptil));
    }
}

class MamaliaOmnivora extends HewanMamalia {
    protected String varomni="dan pemakan segala";
    void TampilkanMamaliaOmnivora() {
        System.out.println("Hewan Omnivora : " + (x+mamalia+varomni));
    }
}

class MamaliaHerbivora extends HewanMamalia {
    protected String varherbi=" dan pemakan Tumbuhan";
    void TampilkanMamaliaHerbivora() {
        System.out.println("Hewan Herbivora : " + (x+mamalia+varherbi));
    }
}

class MamaliaKarnivora extends HewanMamalia {
    protected String varkarni="dan pemakan Daging";
    void TampilkanMamaliaKarnivora() {
        System.out.println("Hewan Karnivora : " + (x+mamalia+varkarni));
    }
}


Tidak ada komentar:

Posting Komentar