• Register
0 votes
185 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();

It will always give

A

B

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

B

But this is completely wrong.

3.9k 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.

38.6k points

Related questions

0 votes
1 answer 82 views
82 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 10.5k points
0 votes
1 answer 60 views
60 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 10.5k points
0 votes
1 answer 20 views
20 views
Problem: Can a subclass access private members of a superclass?
asked May 15 Junia Phoebe 83.8k points
0 votes
0 answers 19 views
1 vote
1 answer 14 views
14 views
Problem: I’m performing this code: ParentClass.variable = value; Is there any alternate method to this ? which one will be better among these?
asked May 8 Raj Tyagi 16k points
0 votes
1 answer 110 views
110 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 3.6k views
3.6k 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 146 views
146 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 10.5k points
0 votes
1 answer 6 views
6 views
Problem: Is there a solution for this : A subclass has what kind of relationship to a superclass?
asked Jun 24 nila 121k points
0 votes
1 answer 7 views
7 views
Problem: Need to facilitate with this : You can always successfully cast a subclass to a superclass.
asked Jun 23 asha 121k points