Vererbung-Konstrucktor
Wenn beim Vererben ein Konstrucktor genutzt werden will, dann müssen nur die Subklassenspezifischen Attribute initialisiert werden. Die Attribute der Superklasse werden mit dem Befehl:
super(x,y);
initialisiert.
Bsp.:
//Superklasse
public class Automobil{
protected int kilometerstand;
protected String farbe;
public Automobil(int pKiloMeter, String pFarbe){
this.kinlometerstand = pKiloMeter;
this.farbe= pFarbe;
}
//Subklasse
public class Motorad extends Automobil{
protected int maxPerson;
protected double oelstand;
public Motorad(int pKilometerstand, String pFarbe,int pMaxPerson, double pOelstand){
super(pKilometerstand, pFarbe)
this.maxPerson = pMaxperson;
this.oelstand = pOelstand;
}
super(x,y);
initialisiert.
Bsp.:
//Superklasse
public class Automobil{
protected int kilometerstand;
protected String farbe;
public Automobil(int pKiloMeter, String pFarbe){
this.kinlometerstand = pKiloMeter;
this.farbe= pFarbe;
}
//Subklasse
public class Motorad extends Automobil{
protected int maxPerson;
protected double oelstand;
public Motorad(int pKilometerstand, String pFarbe,int pMaxPerson, double pOelstand){
super(pKilometerstand, pFarbe)
this.maxPerson = pMaxperson;
this.oelstand = pOelstand;
}
Kommentare
Kommentar veröffentlichen