• Register
0 votes
2.5k 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

Please log in or register to answer this question.

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 27 views
27 views
Problem: URGENT! Any help with this: Implicit super constructor is undefined. must explicitly invoke another constructor
asked Mar 13 Wafa Abu Yousef 6.1k points
0 votes
1 answer 97 views
97 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, 2020 jwilliam 3.9k points
0 votes
1 answer 42 views
42 views asked Oct 19, 2020 SapperIt 1.2k points
0 votes
1 answer 7 views
7 views
Problem : Why do this() and super() have to be the first statement in a constructor? Why does the compiler have these restrictions? Can you give a Any idea where, if the compiler did not have this restriction, something bad would happen?
asked Mar 8 Shovo210 13.3k points
0 votes
1 answer 12 views
12 views
Problem: implicit super constructor object() is undefined
asked Feb 24 Hira 2.2k points
0 votes
1 answer 22 views
0 votes
1 answer 4 views
4 views
Problem: Please can somebody help me with the error I am getting return type specification for invalid?
asked Mar 25 Sana8989 8.9k points
0 votes
1 answer 3 views
3 views
Problem: Can anybody answer me:Constructor vs Destructor
asked Mar 19 Wafa Abu Yousef 6.1k points
0 votes
1 answer 5 views
5 views
Problem: Did anybody face this error, any help: Constructor vs Destructor.
asked Mar 17 Wafa Abu Yousef 6.1k points
0 votes
1 answer 15 views