0

lets say there are two classes A and B, and B is inheriting A.

Now we have not created a constructor for A.

But on the other hand we have created a constructor for B and have not called the A class constructor.

What happens then and Why?

Edit: I created this question mainly thinking about the question in java

  • It depends on the language. Some may *implicitly* call the base constructor, some may require you to do it explicitly. You probably want to specify what language you're interested in. – paxdiablo May 02 '21 at 03:20
  • Does this answer your question? [Will the base class constructor be automatically called?](https://stackoverflow.com/questions/13166019/will-the-base-class-constructor-be-automatically-called). And for java https://stackoverflow.com/questions/34488484/why-is-super-class-constructor-always-called. But the principle is the same for most other oo languages too – derpirscher May 02 '21 at 03:21
  • yes, actually I was thinking about the question in java. This question was in my mind for a while, so I ran different codes to check how it is working. Now that I understand the working clearly, I tried to explain it as best i can, so that others like me would benefit from it. – Arpan Dutta May 02 '21 at 03:45
  • 2
    _Every_ class has a constructor, and _every_ subclass constructor calls a super constructor (even if only implicitly). – chrylis -cautiouslyoptimistic- May 02 '21 at 03:50

2 Answers2

1

It sounds like the code you are describing is something like this:

class A {}

class B extends A {
  B() {}
}

There are two sections of the language spec relevant to the question.

Firstly, Section 8.8.7 says that if the constructor body doesn't begin with this(...) or super(...), then it implicitly starts with super(), an invocation of the superclass' no-arg constructor.

This means B effectively looks like:

class B extends A {
  B() {
    super();
  }
}

(Try comparing the bytecode with and without the explicit super() call, you will see they are identical)

For this to compile, A would have to have a no-arg constructor.

Although it apparently has no constructors, the second relevant bit of the language spec is Section 8.8.9: when no constructors are explicitly declared, a default constructor will be implicitly declared. This has the same access modifier as the class, and take no arguments.

This means A effectively looks like:

class A {
  A() {
    super(); // call to superclass constructor because of Sec 8.8.7, that is, Object().
  }
}

(Again, try declaring this constructor explicitly and comparing the bytecode)

Such a default constructor is necessary in order for a class to invoke its superclass constructor, in order that the instance is fully initialized before use.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

I explained the working as it would be in java

Okay, so I created the question myself as it was a little confusing at first glance. I will try to explain the answer the best I can.

what it does and why it does:

Understanding Situation: Class A is inherited by Class B. But as we haven't created a class A constructor, a default constructor is created.

Now keep in mind that a default constructor doesn't have any parameters in it.

Now as class B has a constructor when its object is created, this constructor is called. As we are not calling the superclass constructor in the subclass constructor, it calls it by default.

Keep in mind that if it calls a superclass constructor by default, it will not pass any parameters while calling that constructor. You can assume that it places a super(); on the first line of the B class constructor.

As Jonathan Rosenne states:

The Java Language Specification says "If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.". Note end of the sentence - no arguments

Final answer: Now, as a superclass constructor is called without any parameter, and it has already created a default constructor in the superclass without any parameters, this default constructor is called by the subclass constructor.

Default class constructor does its work and gives all the variables their default values, then passes the control back to the subclass constructor and it does anything that we have mentioned in the subclass constructor.

Solving other problems by inferring this answer:

  • 1.So if you have created a constructor in the superclass but it doesn't have any parameters, then you might or might not call it in the subclass constructor, the result would be the same. Either you would have to write super(); or it would be added by default at the first line of the subclass constructor.

  • 2.If you have a superclass constructor that has parameters, then you must call it in the subclass constructor, else it would by default call a superclass constructor without any parameters and as we have created a parameterized constructor in the superclass, it would not have a default or a non-parameterized constructor to get called and it would result in an error of constructor not found.

  • I hope it explains everything that needs to be understood regarding this matter, even though the concept is very simple, while looking at the question, It might look overwhelming. So I would suggest to keep calm and go through the problem step by step to get to the final conclusion. – Arpan Dutta May 02 '21 at 04:00
  • I suggest your answer would be strengthened by referencing the [Java Language Specification](https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-ConstructorBody) which says "If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.". Note end of the sentence - **no arguments**. – Jonathan Rosenne May 02 '21 at 04:29
  • @JonathanRosenne Thanks for your reply, I made an edit and included your helpful information in the answer. – Arpan Dutta May 02 '21 at 04:48