Pages

Konsep PBO Polimorfisme Bag-1


//--------------------
//mahasiswa.java
//--------------------



package polimorfism;

public class manusia {

    public String nama;

    public manusia(String n) {
        this.nama = n;
    }

    public String tampilkanNama() {
        return nama;
    }

    public void makan() {
        System.out.println("Nyam... nyam... nyam...");
    }

    public void kerja() {
        System.out.println("Kerja...kerjaaa...");
    }
}
//--------------------------------------------------------

//--------------------

//pekerja.java
//--------------------
package polimorfism;

public class pekerja {

    public static void main(String[] args) {
        manusia[] profesi = new manusia[3];
        profesi[2] = new sopir("Dedi");

        for (int i = 0; i < 3; i++) {
            profesi[i].kerja();
        }
    }
}

//--------------------------------------------------------

//--------------------

//sopir.java
//--------------------
package polimorfism;

public class sopir extends manusia {

    public sopir(String n) {
        super(n);
    }

    @Override
    public void kerja() {
        System.out.println("Ngung...ngung...ngung...ciiit...");
    }
}

//--------------------------------------------------------

//--------------------

//dedi.java
//--------------------
package polimorfism;

public class dedi {

    public static void main(String[] args) {
        sopir dedi = new sopir("Dedi");
        System.out.println("Nama= " + dedi.tampilkanNama());
        dedi.makan();
        dedi.kerja();
    }
}
//--------------------
//output
//--------------------
Nama= Dedi
Nyam... nyam... nyam...
Ngung...ngung...ngung...ciiit...

0 comments:

Posting Komentar