-1

As we know super() method is basically used to access the superclass constructor which is possible only by using extend keyword which promotes the inheritance concept so how can we say that constructor is not inherited?.

daniu
  • 14,137
  • 4
  • 32
  • 53
  • 1
    you can try adding a println in your constructor to see if the parent constructor gets called or not. – Angel Koh Apr 17 '18 at 06:43

3 Answers3

2

As the Java doc says,

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Extracted from here, read more:

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
1

Constructor is a block of code that allows you to create an object of class and has same name as class with no explicit return type.

Whenever a class (child class) extends another class (parent class), the sub class inherits state and behavior in the form of variables and methods from its super class but it does not inherit constructor of super class

Reasons:

  • Constructors are special and have same name as class name.
  • A constructor cannot be called as a method. It is called when object of the class is created
  • A parent class constructor is not inherited in child class and this is why super() is added automatically in child class constructor if there is no explicit call to super or this.
Rizwan
  • 2,369
  • 22
  • 27
0

I think what you're talking about is called constructor chaining. Now What is Constructor Chaining

Constructor chaining occurs through the use of inheritance. A subclass constructor method's first task is to call its superclass' constructor method. This ensures that the creation of the subclass object starts with the initialization of the classes above it in the inheritance chain.

There could be any number of classes in an inheritance chain. Every constructor method will call up the chain until the class at the top has been reached and initialized. Then each subsequent class below is initialized as the chain winds back down to the original subclass. This process is called constructor chaining.

Source-(https://www.thoughtco.com/constructor-chaining-2034057)

Sr7
  • 86
  • 1
  • 9