I am a beginner in java programming language, recently I have studied that constructors can not be inherited in java, Can anyone please explain why?
I have already read this link of C++
I am a beginner in java programming language, recently I have studied that constructors can not be inherited in java, Can anyone please explain why?
I have already read this link of C++
Only fields, methods, and nested classes are the membe of any class not Constructors. A subclass inherits all the members like (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.
In simple words, a constructor cannot be inherited, since in subclasses it has a different name (the name of the subclass).
You can do only:
Methods, instead, are inherited with "the same name" and can be used.
As for the reason: It would not have much sense to inherit a constructor, since constructor of class A means creating an object of type A, and constructor of class B means creating an object of class B.
You can still use constructors from A inside B's implementation though:
new
. It cannot be called as a method.So inheritance is practically not possible as such. However in a construct one might call other constructors.
this(...)
;super(...)
;Example
There unfortunately is no possibility to use A's constructor for a boolean:
The language designes could have implemented such a thing as:
Generate for every public constructor in the base class, a constructor with the same signature if such a constructor is not defined already. Call
super
with the same parameters. Callthis()
if there is a default constructor.That seems a bit bloating the code. And is not simply a pointer in a virtual method table, by which method inheritance/overriding works.
Constructors are not members of classes and only members are inherited. You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses.
Syntactic limitations can often be circumvented should there be any reason for a feature in a conceptual way. With this, I believe, the real reason for not supporting inheritance of constructor is not due to syntactic limitations but rather due to semantics.
Conceptually inheritance provides a mechanism to acquire (or inherit) a behavior and most likely without writing any piece of code since its purpose is to provide code-reuse. For a child class, it makes no case to inherit the behavior of initialization of its parent class. After all, an inherited behavior finds its best use when an external caller can use it without knowing who (in the parent chain) actually has implemented it. As you can see, a caller hardly has any business knowing how a parent class is initialized via its child class, there is no discernible reason for supporting inheritance for a (parent class’s) constructor.
Reason mentioned in docs of Inheritance
You can refer docs of Providing Constructors for Your Classes