Sebelumnya saya mengshare tentang polimorfisme di java, kali ini saya akan kasi contoh program yg lebih seru biar lebih paham apa itu polimorfisme,
simpan dengan nama Student.java
simpan dengan nama Student.java
class Student {
public void Write(int ID, int Grad, String Fname, String Lname) {
m_ID = ID;
m_Graduation = Grad;
m_First = Fname;
m_Last = Lname;
}
public void Display()
{
System.out.println( "Student: " + m_ID + " " + m_First + " " + m_Last + " Graduated: " + m_Graduation);
}
private int m_ID, m_Graduation;
private String m_First;
private String m_Last;
}
public void Write(int ID, int Grad, String Fname, String Lname) {
m_ID = ID;
m_Graduation = Grad;
m_First = Fname;
m_Last = Lname;
}
public void Display()
{
System.out.println( "Student: " + m_ID + " " + m_First + " " + m_Last + " Graduated: " + m_Graduation);
}
private int m_ID, m_Graduation;
private String m_First;
private String m_Last;
}
untuk yang ini simpan dengan nama StudentGrad.java
class StudentGrad extends Student {
public void Write(int ID, int Grad, String Fname, String Lname, int yrGrad, String unSch, String major)
{
super.Write(ID, Grad, Fname, Lname);
m_UndergradSchool = unSch;
m_Major = major;
m_Grad = Grad;
YearGraduated = yrGrad;
}
public void Display(){
super.Display();
System.out.println(" Graduated: " + m_Grad + " " + m_UndergradSchool + " " + m_Major + " " + YearGraduated);
}
private Integer YearGraduated,m_Grad;
private String m_UndergradSchool;
private String m_Major;
}
public void Write(int ID, int Grad, String Fname, String Lname, int yrGrad, String unSch, String major)
{
super.Write(ID, Grad, Fname, Lname);
m_UndergradSchool = unSch;
m_Major = major;
m_Grad = Grad;
YearGraduated = yrGrad;
}
public void Display(){
super.Display();
System.out.println(" Graduated: " + m_Grad + " " + m_UndergradSchool + " " + m_Major + " " + YearGraduated);
}
private Integer YearGraduated,m_Grad;
private String m_UndergradSchool;
private String m_Major;
}
dan yang ini simpan dengan nama file StudentInfo.java
class StudentInfo {
public static void main (String args[]) {
StudentGrad myStudent = new StudentGrad();
myStudent.Write(10,1,"Bob","Smith", 2000,"Columbia University","CS");
myStudent.Display();
}
}
public static void main (String args[]) {
StudentGrad myStudent = new StudentGrad();
myStudent.Write(10,1,"Bob","Smith", 2000,"Columbia University","CS");
myStudent.Display();
}
}
Ini adalah hasil runningnya
ni program tugas akhir praktikum ane, silahkan dicoba jg ^_^
/*disimpan nama Hewan.java*/class Hewan {String nama, jenis, bersuara;Hewan(String nama, String jenis, String bersuara){//sintaks menampilkan namathis.nama=nama;this.jenis=jenis;this.bersuara=bersuara;}public String toString(){return "Ini adalah "+jenis+"\nNamanya "+nama+"\n"+bersuara;}public void bersuara(){}}
/*disimpan nama Polimorfisme.java*/import java.util.*;class Polimorfisme{public static void main(String args[]){Hewan[] hewannya = {new Anjing("Broni", "Herder", "Hug Hug Hug......"),new Kucing("Kiti", "Persia", "Meong.. Meong... "),new Ayam("Toki", "Buras", "kuk kruyu. ")};Hewan pilihHewan;Random pilih = new Random();for (int i = 0; i<5 ; i++){pilihHewan = hewannya[pilih.nextInt(hewannya.length)];System.out.println("\nPilihan Anda:\n" + pilihHewan);}}}
/*disimpan nama Ayam.java*/class Ayam extends Hewan{private String nama,jenis,bersuara;Ayam(String nama, String jenis, String bersuara){//sintaks menampilkan namasuper(nama, jenis, bersuara);}public void ShowAyam(){System.out.println("Nama Ayam : "+nama);System.out.println("Jenis Ayam : "+jenis);
//sintaks menampilkan nama dan jenis hewan
}public void bersuara(){//sintaks menampilkan suara hewanSystem.out.println("kuk kruyu. ");}
}
/*disimpan nama Kucing.java*/class Kucing extends Hewan{private String nama,jenis,bersuara;Kucing(String nama, String jenis, String bersuara){//sintaks menampilkan namasuper(nama, jenis, bersuara);}public void ShowKucing(){System.out.println("Nama Kucig : "+nama);System.out.println("Jenis Kucing : "+jenis);//sintaks menampilkan nama dan jenis hewan}public void bersuara(){//sintaks menampilkan suara hewanSystem.out.println("Meong.. Meong... ");}}
/*disimpan nama Anjing.java*/class Anjing extends Hewan{private String nama,jenis,bersuara;Anjing(String nama, String jenis, String bersuara){//sintaks menampilkan namasuper(nama, jenis, bersuara);}public void ShowAnjing(){System.out.println("Nama Anjing : "+nama);System.out.println("Jenis Anjing : "+jenis);//sintaks menampilkan nama dan jenis hewan
}public void bersuara(){//sintaks menampilkan suara hewanSystem.out.println("Hug Hug Hug...... ");}
}
2 comments:
coment tang endi' teh....
Jelasin donk gan programya, agak kurang ngerti.. :)
Kunjungi blog ane ya gan. top-ilmu.blogspot.com. thx
Post a Comment