• Register
0 votes
1.1k views

Problem :

I am currently working on the project and i am getting the error as below :

"Implicit super constructor MyPerson() is undefined. Must explicitly invoke another constructor"

But I am unable to understand it.

Below is my MyPerson class:

public class MyPerson {
    public MyPerson(String myname, double myDOB){
    }
}

And my MyStudent class while trying to implement the MyPerson class, and just giving it an instructor variable as below :

public class MyStudent extends MyPerson {
    public MyStudent(String myInstructor) {
    }
}
6 5 3
7,540 points

1 Answer

0 votes

Solution :

If the constructor does not explicitly invoke the superclass constructor in that case the Java compiler automatically inserts the call to the no-argument constructor of the superclass.

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

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

Your superclass MyPerson does not have the no-arg constructor. So either try to supply an explicit no-arg constructor in MyPerson or explicitly call your existing superclass constructor in the MyStudent constructor.

9 7 4
38,600 points

Related questions

0 votes
1 answer 58 views
58 views
Problem : I have the below two classes public class A {     A() {         System.out.println("A");     } } class B extends A {     B() {         System.out.println("B");     } } And after that running 1 A c = new B(); or 2 B c = new B( ... glance, in either of the scenario, I would assume that only a B constructor would be called and so the only output would be B But this is completely wrong.
asked Jan 23 jwilliam 3.9k points
0 votes
1 answer 9 views
0 votes
1 answer 10 views
0 votes
1 answer 9 views
0 votes
1 answer 6 views