What does "constructors not inherit" mean?

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

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

Doesn't the superclass constructor (without arguments) automatically invoke the inheritance form?

Does the fact that "constructors are not inherited" exactly mean?

+4
source share
2 answers

This means that you cannot create a subclass using the superclass constructor unless the subclass also declares it. Example:

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

Now you cannot do this:

 B b = new B("testing"); 
+9
source

This means that your superclass has a constructor; it does not mean that the subclass will automatically receive the same constructor; You must define it manually.

The default constructor is an exception, but the view is not. It is automatically determined for you, but it is not really "inherited" because it is still part of a subclass; he is not a member of the superclass.

+6
source

Source: https://habr.com/ru/post/1344826/


All Articles