1

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.

1
  • Angestrellter doesn't have non-parametric constructor. So you must call super(String v, String n, int a) in your constructor Boss Commented Mar 18, 2015 at 11:01

2 Answers 2

4

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Fine, thanks... but that's not mentioned in the book -.-
0

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 !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.