• Register
0 votes

Problem :

I have the below two classes

public class A {

    A() {




class B extends A {

    B() {




And after that running


A c = new B();



B c = new B();

It will always give



Why is this happening to my code? At a first glance, in either of the scenario, I would assume that only a B constructor would be called and so the only output would be


But this is completely wrong.

7 5 2
3,870 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

With super(), a superclass no-argument constructor is called and with super(parameter list), a superclass constructor with the matching parameter list is called.

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

If the subclass constructor invokes the constructor of its superclass, either explicitly or implicitly, you might think that there will be the whole chain of the constructors called, all the way back to a constructor of Object. In fact, this is a case. It is called the constructor chaining, and you must be aware of it when there is such long line of class descent.

9 7 4
38,600 points

Related questions

0 votes
1 answer 38 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 9.2k points
0 votes
1 answer 36 views
Problem: Hello Programmers! I just gave an interview for a programming job. The interviewer asked me a question regarding inheritance in Java. The question was: Can subclass access the private members of a superclass? I answered: No!. But, I am still confused about whether I answered correctly or not. Can any of you tell me whether my answer was correct or not? Also, explain why and why not?
asked Dec 17, 2020 Code Learner 9.2k points
0 votes
0 answers 16 views
0 votes
1 answer 34 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 9.2k points
0 votes
1 answer 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 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 ... and just giving it an instructor variable as below : public class MyStudent extends MyPerson { public MyStudent(String myInstructor) { } }
asked Dec 16, 2019 alecxe 7.5k points
0 votes
1 answer 21 views
Problem: Which of the following is not a type of constructor? Copy constructor Friend constructor Default constructor Parameterized constructor Also, explain each type of constructor. Thanks!
asked Mar 6 Code Learner 9.2k points
0 votes
1 answer 2 views
Problem: I'm on the lookout for any help how to know which of the following is not a type of constructor?
asked Apr 1 zayed1 24.7k points
1 vote
1 answer 2 views
Problem: Can someone please help fix this uncaught error : what key word can you use to call a superclass constructor explicitly?
asked 4 days ago MUHAMMAD MUNEEB 80k points
0 votes
1 answer 1 view
1 view
Problem: Please assist me in resolving this issue: how to call a superclass method from subclass in java
asked 19 hours ago tuhin1 46.8k points