Reputation: 73
Can anybody tell me why this code isn't correct?
public class Boss extends Angestellter {
Boss(String v, String n, int a) { // ERROR **
vorname = großKleinSchreibung(v);
nachname = großKleinSchreibung(n);
alter = a;
}
}
** Implicit super constructor Angestellter() is undefined. Must explicitly invoke another constructor
public class Angestellter {
protected String vorname;
protected String nachname;
public int alter;
Angestellter(String v, String n, int a) {
this.vorname = großKleinSchreibung(v);
this.nachname = großKleinSchreibung(n);
this.alter = a;
}
I dont find the error, because its exactly how its explained in the book which im using to learn oop with java.
Upvotes: 1
Views: 64
Reputation: 2642
In simple words
You cannot override constructor of super class in JAVA
Here is your little modified code !!
public class Angestellter {
protected String vorname;
protected String nachname;
public int alter;
Angestellter(String v, String n, int a) {
this.vorname = großKleinSchreibung(v);
this.nachname = großKleinSchreibung(n);
this.alter = a;
}
...
}
public class Boss extends Angestellter {
... Other methods
}
// In main
Angestellter myObj = new Boss("asd","as",1); // It will call constructor itself ... because it is inherited !!
Upvotes: 0
Reputation: 393771
You should call the constructor of the base class explicitly, since if you don't, the compiler adds an implicit call to the parameterless constructor of the base class, which doesn't exist in your case.
public class Boss extends Angestellter {
Boss(String v, String n, int a) {
super (v,n,a);
vorname = großKleinSchreibung(v);
nachname = großKleinSchreibung(n);
alter = a;
}
}
Upvotes: 4