0

I'm trying to implement the constructor of a class that has more parameters than the parent class, the only one in common is the title. When I try to implement the constructor in the Book class, it shows me an error "Implicit super constructor Item() is undefined".

public class Book extends Item {

private String author = "";
private String ISBN = "";
private String publisher = "";

public Book(String theTitle, String theAuthor, String theIsbn, String thePublisher){

}

}

Parent class constructor;

public abstract class Item {

private String title;
private int playingTime;
protected boolean gotIt;
private String comment;

public Item(String title, int playingTime, boolean gotIt, String comment) {
    super();
    this.title = title;
    this.playingTime = playingTime;
    this.gotIt = gotIt;
    this.comment = comment;
}

Thanks in advance.

3 Answers3

5

Your super class doesn't have an no-args default constructor, so you have to explicitly invoke the super class's overloaded constructor using super() keyword passing the default values.

public Book(String theTitle, String theAuthor, String theIsbn, String thePublisher){
super(thTitle,0,false,null)
}
PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

because you have not defined no-argument constructor, or provide parameters in super(....)

neonleo
  • 194
  • 1
  • 2
0

java doesn't added no-argument constructor if you are adding constructor with one or more parameter. Java adds no-argument constructor if we don't add any constructor in class. Other way around for your problem is you need to overload the constructor like

public Item(String title, int playingTime, boolean gotIt, String comment) {
    super(); //remove this constructor or define no-arg constructor in super class.
    super(thTitle,0,false,null); //add this constructor
}

YouNeedToReadHeadFirstCoreJava

AmitG
  • 10,365
  • 5
  • 31
  • 52