• Register
0 votes
1.7k 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 74 views
74 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 27 views
27 views asked Oct 19, 2020 SapperIt 1.2k points
0 votes
1 answer 16 views
0 votes
1 answer 12 views
0 votes
1 answer 14 views
14 views asked Oct 14, 2020 nomi
0 votes
2 answers 20 views
20 views
What is Constructor and Destructor in C# explain with example?
asked Sep 28, 2020 Sofi55 1.1k points
0 votes
1 answer 16 views
16 views
Problem: I have spent much of the time doing programming in Java. In Java you call the class you Inherited from with the super(); keyword. I know everybody knows that. Now I have the class in C++ which has the default constructor which takes some of the arguments. For ... to know is there something like super() in C++ or do I need to define the function where I am initializing all the variables?
asked Sep 3, 2020 Raphael Pacheco 4.9k points
1 vote
1 answer 16 views
16 views
Problem: Hello Kodlogs, I am a Java programmer. I recently shifted to Python and willing to learn this programming language alongside Java. I am posting to know about the constructors in Python. I’ve read the documentation superficially but I like human conversation ... a detail explanation of Python constructors from you. I hope you guys will have fun answering my question. Thanks a lot.
asked Jun 30, 2020 adamSw 11.3k points
0 votes
1 answer 18 views
18 views
Problem: I just recently started learning inheritance in Java. I am a bit confused between method overloading and method overriding. If two classes have same method with different number of arguments, given that the classes have parent-child relationship, will it be overloading or overriding? Moreover, when does the overloading occurs in inherited classes?
asked Jan 8 Code Learner 5.5k points
0 votes
1 answer 13 views
13 views
Problem: Hi there! I recently attempted an exam of Programming in Java. There was a statement that was to be marked as true or false and I had to justify my answer. The statement was: A protected member of a class may be directly accessed by ... concepts in programming and I am not sure whether I understand inheritance properly. Can anybody here help me clear my concepts more accurately? Thanks
asked Dec 27, 2020 Code Learner 5.5k points