employee.java
package personal;
public class employee extends Person{
private String position;
public employee(String name, String position){
this.name = name;
this.position = position;
}
public void getName(String name){ //method harus sama antara superclass dan ssubclass
System.out.println("Nama Pegawai : "+name);
System.out.println("Posisi : "+position);
}
}
Person.java
package personal;
public class Person {
protected String name;
protected void getName(){
System.out.println("Perusahaan Indonesia Satu");
}
}
polimorfismetest.java
package personal;
public class polimorfismetest {
public static void main(String[] args) {
Person ref;
Student S1 = new Student("Plastik");
String name = "Bungkus";
Student S2 = new Student(name);
employee E1 = new employee("Kertas","Maling");
E1.getName();
ref=S1;
ref.getName();
ref=S2;
ref.getName();
ref=E1;
E1.getName();
E1.getName("D");
}
}
Student.java
package personal;
public class Student extends Person {
public Student(String name) {
this.name = name;
}
public void getName(){
System.out.println("Nama Siswa : "+ name);
}
}
Beri Komentar Tutup comment