Inheritance

Berikut ini merupakan salah satu contoh penggunaan inheritance dalam pemrograman berbasis objek


Class Item:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class Item {
    private String title;
    private int playingtime;
    private boolean gotit;
    private String comment;
    public Item(String title, int playingtime) //constructor
    {
        this.title=title;
        this.playingtime=playingtime;
        this.gotit=false;
        this.comment = "";
    }
    public void setComment(String comment)//method untuk memasukkan comment item
    {
        this.comment = comment;
    }
    public String getComment() //method untuk return comment item
    {
        return this.comment;
    }
    public void setGotIt(boolean gotit) //method untuk memasukkan ketersediaan item
    {
        this.gotit = gotit;
    }
    public boolean getGotIt() //method untuk return ketersediaan item
    {
        return this.gotit;
    }
    public void Print() //method untuk mencetak item
    {
        System.out.println("Title: " + title + "( " + playingtime + " mins )" );
        //cek item tersedia atau tidak
        if(gotit)
        {
            System.out.println("Available");
        }
        else
        {
            System.out.println("Not Available");
        }
        System.out.println(comment);
    }
}

Class CD:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class CD extends Item
{
    private String artist;
    private int numberoftracks;
    public CD(String title, int playingtime, String artist, int numberoftracks) //constructor
    {
        super(title,playingtime);
        this.artist=artist;
    }
     
    public void setComment(String comment) //method untuk memasukkan comment CD
    {
        super.setComment(comment);
    }
    public void setGotIt(boolean gotit) //method untuk memasukkan nilai ketersediaan CD
    {
        super.setGotIt(gotit);
    }
    public String getArtist() //method untuk return nama artist
    {
        return artist;
    }
    public int getNumber() //method untuk return jumlah tracks
    {
        return numberoftracks;
    }
    public void printArtist() //method untuk mencetak nama artist
    {
        System.out.println("Artist: " + artist);
    }
}



Class DVD:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public class DVD extends Item
{
    private String director;
    public DVD(String title, int playingtime, String director) //constructor
    {
        super(title,playingtime);
        this.director=director;
    }
    public void setComment(String comment) //method untuk memasukkan comment DVD
    {
        super.setComment(comment);
    }
    public void setGotIt(boolean gotit) //method untuk memasukkan nilai ketersediaan DVD
    {
        super.setGotIt(gotit);
    }
    public String getDirector() //method untuk return nama director
    {
        return director;
    }
    public void print() //method untuk mencetak nama director
    {
        System.out.println("Director: " + director);
    }
}


Class Database:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.ArrayList;
 
public class Database
{
    private ArrayList<Item>barang; //declare array list dengan tipe Item
    public Database() //constructor
    {
        barang = new ArrayList<Item>();
    }
    public void addItem(Item items) //method untuk menambah item
    {
        barang.add(items);
    }
    public void removeItem(Item items) //method untuk meremove item
    {
        barang.remove(items);
    }
    public void Print() //method untuk mencetak arraylist item
    {
        for(Item b: barang )
        {
            b.Print();
            if(b instanceof CD) //jika item b merupakan CD maka mencetak nama artis dan jumlah tracks
            {
                CD a = (CD) b;
                a.Print();
             }
            else
            {
                DVD a = (DVD) b; //jika item b merupakan CD maka mencetak nama director
                a.Print();
            }
            System.out.println();
        }
    }
     
}







Komentar

Postingan Populer