Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "constructors are not inherited" exactly mean?

Tags:

java

I read somewhere that in Java "constructors are not inherited".

On the other hand, I also read that if I don't explicitly call super, Java automatically calls the super class constructor with no arguments (such a constructor must exist in this case).

Isn't automatically calling the super class constructor (with no arguments) a form of inheritance?

What does "constructors are not inherited" exactly mean?

like image 837
an00b Avatar asked Dec 07 '22 22:12

an00b


1 Answers

It means that you cannot create a subclass, using a constructor of the super class-if the subclass did not also declared it. An example;

class A {
  A() {}
  A(String s) {}
}

class B extends A {    
}

Now you cannot do this:

B b = new B("testing");
like image 141
Erkan Haspulat Avatar answered Dec 10 '22 13:12

Erkan Haspulat