INHERITANCE
Inheritance is actually what?
Inheritance is a big advantage in object-based programming because of a property or method defined in the superclass, this property is automatically inherited from all subclasses.Superclass is a parent class that can be bequeathed to the subclass. And what about super classs or superclass defines this?
The way you just simply use the keyword "extends"
Well this is an example of a super class:

public class DokterUmum{
public String kodeDokter,namaDokter;
public double biayaObat,biayaPeriksa;
public DokterUmum(){
this.kodeDokter="e4500";
this.namaDokter="Ulil";
this.biayaObat=45000.0;
this.biayaPeriksa=40000.0;
}
public void setNamaDokter(String namaDokter){
this.namaDokter=namaDokter;
}
public void setKodeDokter(String kodeDokter){
this.kodeDokter=kodeDokter;
}
public void setBiayaObat(double biayaObat){
this.biayaObat=biayaObat;
}
public void setBiayaPeriksa(double biayaPeriksa){
this. biayaPeriksa= biayaPeriksa;
}
public String getKodeDokter(){
return kodeDokter;
}
public String getNamaDokter(){
return namaDokter;
}
public double getBiayaObat(){
return biayaObat;
}
public double getBiayaPeriksa(){
return biayaPeriksa;
}
public double TotalBiaya(){
return(biayaObat+biayaPeriksa);
}
}

Setelah decompile maka hasilnya seperti ini :






Do not worry about these results do not mean the program you are wrong .... ok
Because that was the response from java to the super class.
Well this is an example of a subclass program:

class DokterJantung extends DokterUmum
{
public double biayaSpesialis;
public String titleBelakang;
public DokterJantung(){
this.biayaSpesialis=50000.0;
this.titleBelakang="Maria SP.J";
}
public void setBiayaSpesialis(double biayaSpesialis)
{
this.biayaSpesialis=biayaSpesialis;
}
public void setTitleBelakang(String TitleBelakang)
{
this.titleBelakang=TitleBelakang;
}

public double getBiayaSpesialis(){
return biayaSpesialis;
}

public String getTitleBelakang(){
return titleBelakang;
}
public double TotalBiaya(){

return (biayaObat+biayaPeriksa+biayaSpesialis);
}
public static void main (String[]args){
DokterJantung dj = new DokterJantung();
dj.setBiayaSpesialis(70000.0);
dj.setTitleBelakang("Sarwinto SP.J");
System.out.println("Biaya Spesialis =" +dj.getBiayaSpesialis());

System.out.println("Nama Dokter =" +dj.getTitleBelakang());
double TotalBiaya=dj.TotalBiaya();
System.out.println("Total Biaya =" +TotalBiaya);
}
}

These are the results compiled from the subclass is above the super classnya DokterUmum


Well this is an example of a subclass program:...

Comments