Breaking News
recent

Pewarisan Jamak (Multiple Inheritance) OOP

Assalamualaikum... lama udah ga posting gue, soalny gue agak sibuk karena abis UTS dan lagi edit-edit video buat channel youtube gue 
Pewarisan Jamak atau Multiple Inheritance memungkinkan sebuah kelas mewarisi data bahkan method lebih dari satu kelas induk.

Pertama-tama buka aplikasi Net Beans dan kemudian
bikin project baru dengan nama DemoPacket.java

Setelah itu ketikkan Multiple Inheritance dalam Java --> Project DemoPaket.java

Buku.java :

masukkan kode berikut ini

package demopaket;



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Ahmad Akbar Mariuddin
 */
class Buku
{
    String judul, pengarang;
    long hargaBuku;
    public Buku(String judul, String pengarang, long hargaBuku)
    {
        this.judul=judul;
        this.pengarang=pengarang;
        this.hargaBuku=hargaBuku;
    }
    public void cetakBuku()
    {
        System.out.println("\nJudul : "+judul);
        System.out.println("Pengarang   : "+pengarang);
        System.out.println("Harga Buku : Rp "+hargaBuku);
        System.out.println();
    }
    }
   
CD.java

package demopaket;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Ahmad Akbar Mariuddin
 */
class CD 
{
    String ukuran;
    long hargaCD;
    public CD(String ukuran, long hargaCD)
    {
        this.ukuran=ukuran;
        this.hargaCD=hargaCD;        
    }
    public long getHargaCD()
    {
        return hargaCD;
    }
    public void cetakCD()
    {
        System.out.println("Ukuran CD : "+ukuran);
        System.out.println("Harga CD : Rp "+hargaCD);
        System.out.println();
    }
}

ChildCD.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package demopaket;

/**
 *
 * @author Barni
 */
public class ChildCD extends CD implements InterfaceCD 
{
    public ChildCD(String ukuran, long hargaCD)
    {
        super(ukuran,hargaCD);
    }
    
}

DemoPaket.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package demopaket;

/**
 *
 * @author Barni
 */
public class DemoPaket {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Paket a=new Paket("Pemrograman Berorientasi Objek", "Benyamin Langgu Sinaga",60000,"700 MB",50000);
        a.hitungHargaPaket();
        a.cetakPaket();
    }
    
}

InterfaceCD.java : klik kanan dan pilih project baru dan pilih interface

package demopaket;


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Barni
 */
interface InterfaceCD
{
    void cetakCD();
    long getHargaCD();
}

Paket.java

package demopaket;



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Barni
 */
class Paket extends Buku implements InterfaceCD
{
 long hargaPaket;
 InterfaceCD interfaceCD;
 public Paket(String judul, String pengarang, long hargaBuku, String ukuran, long hargaCD)         
 {
     super(judul, pengarang, hargaBuku);
     interfaceCD = new ChildCD(ukuran, hargaCD);
 }
 public void hitungHargaPaket()
 {
     hargaPaket=super.hargaBuku + getHargaCD();
 }
 public void cetakCD()
 {
     interfaceCD.cetakCD();
 }
 public long getHargaCD()
 {
     return (interfaceCD.getHargaCD());
 }
 public void cetakPaket()
 {
    super.cetakBuku();
    cetakCD();
    System.out.println("Harga paket Buku dan CD : Rp "+hargaPaket+"\n");
}
}


Setelah semua sudah kalian buat, maka selanjutnya yaitu clean project kalian dan run
Semoga berhasil


Septiceeye2

Septiceeye2

No comments:

Post a Comment

Powered by Blogger.