0

I am trying to inherite to a subclass from class LesEenheid to subclass Vak. But I immediately get a message saying : There is no defautl constructor available in com.company.LesEenheid.

package com.company;

public class LesEenheid {
    private String naam;
    private int ects;
    private int studiejaar;

    public LesEenheid(String lesNaam, int lesEcts, int lesStudiejaar){
        naam = lesNaam;
        ects = lesEcts;
        studiejaar =lesStudiejaar;
    }

    public String toString(){
        return naam + " " + ects + " " + studiejaar;
    }
}

public class Vak extends LesEenheid {



}

Pakjethee
  • 11
  • 2
  • Java will only create a default constructor if there are no explicit constructors. Since you defined a constructor, you should define a default constructor – dustytrash Mar 31 '19 at 17:09
  • Well, the error is quite clear - there is no default constructor. Instead you have one with parameters: `public LesEenheid(String lesNaam, int lesEcts, int lesStudiejaar)`, so your subclass will have to call it instead. Just create a constructor in your subclass and call `super(a,b,c,d, etc)` inside it – Svetlin Zarev Mar 31 '19 at 17:10
  • Possible duplicate of [Java default constructor](https://stackoverflow.com/questions/4488716/java-default-constructor) – Svetlin Zarev Mar 31 '19 at 17:11
  • Possible duplicate of [When do superclasses not have a default constructor?](https://stackoverflow.com/questions/48392818/when-do-superclasses-not-have-a-default-constructor) – Pino Mar 31 '19 at 17:13

2 Answers2

0

You must have the same constructor defined in the parent class, in your case:

public Vak (String lesNaam, int lesEcts, int lesStudiejaar) {

}

If you want to use the default constructor you need to include it in the superclass:

public LesEenheid() {

}
nortontgueno
  • 2,553
  • 1
  • 24
  • 36
0

You just have to call Super class (LesEenheid) constructor, in that way you will pass arguments to superclass constructor and initialize superclass variables

package com.company;

public class LesEenheid {
    private String naam;
    private int ects;
    private int studiejaar;

    public LesEenheid(String lesNaam, int lesEcts, int lesStudiejaar){
        naam = lesNaam;
        ects = lesEcts;
        studiejaar =lesStudiejaar;
    }

    public String toString(){
        return naam + " " + ects + " " + studiejaar;
    }
}

public class Vak extends LesEenheid {

    public Vak(String lesNaam, int lesEcts, int lesStudiejaar)
    {
        Super(lesNaam, lesEcts, lesStudiejaar);
    }
}
pavelbere
  • 964
  • 10
  • 21