javaclassconstructorsuperclass

implicit super constructor Person() is undefined. Must explicitly invoke another constructor?


I am working on a project and i am getting the error "implicit super constructor Person() is undefined. Must explicitly invoke another constructor" and i don't quite understand it.

Here is my person class:

public class Person {
    public Person(String name, double DOB){

    }
}

And my student class when trying to implement the person class, and giving it an instructor variable.

public class Student extends Person {

    public Student(String Instructor) {

    }

}

Solution

  • If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.

    If the super class does not have a

    no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem.

    Reference: http://docs.oracle.com/javase/tutorial/java/IandI/super.html : (See under section 'SubClass Constructors')

    So whenever dealing with parameterized constructors make a super(parameter1, parameter2 ..) call to the parent constructor. Also this super() call should be the FIRST line in your constructor block.

    Or else, if a scenario demands, create an additional non-parameterized () constructor in the parent class