Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't Constructor Inherited?

As a follow up Question, to Base Class Only Methods

Parent Methods are inherited by their descendants.

How come then Constructors (eg New()) aren't? Which to me seem to break inheritance.

Is there an attribute somewhere that marks it as special? (If so what is it?)

Could so one please explain what going on.

like image 937
Adam Speight Avatar asked Feb 20 '12 15:02

Adam Speight


People also ask

Why constructor is never inherited?

Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Is constructors are inherited?

No, constructors cannot be inherited in Java. In inheritance sub class inherits the members of a super class except constructors. In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors.

Why are constructors not inherited in C++?

Constructors are different from other class methods in that they create new objects, whereas other methods are invoked by existing objects. This is one reason constructors aren't inherited.

What happens to constructor in inheritance?

Constructors are not inherited. The superclass constructor can be called from the first line of a subclass constructor by using the keyword super and passing appropriate parameters to set the private instance variables of the superclass.


3 Answers

I suspect the real reason is twofold:

First of all, no static (Shared) members are inherited in VB# (or C#). While this is in line with what most OO languages do, it’s not a necessary design. It could conceivably have been implemented differently.

Secondly, it often makes sense to restrict the set of constructors that can construct a child object. In particular, since a child class often has additional members that need to be initialised, invoking a parent class constructor would leave the child in an uninitialised state. Imagine constructors could be inherited and tell me what the following code is supposed to do:

Class Base
    Public Sub New()
    End Sub
End Class

Class Derived : Inherits Base
    Public Property X() As Integer

    Public Sub New(ByVal value As Integer)
        X = value
    End Sub
End Class

' …

Dim foo As New Derived()
Console.WriteLine(foo.X) ' = ???

Given this, it makes a good deal of sense to forbid inheritance of constructors.

like image 57
Konrad Rudolph Avatar answered Oct 01 '22 20:10

Konrad Rudolph


Inheritance (or, to be precise, a subtype relationship) guarantees that if S is a subtype of T, you can use an object of type S whenever an object of type T is required.

Constructors can never be executed on an object -- they are executed to create an object. They are special in that way:

myS.SomeMethodOfT()    ' works
myS.New()              ' doesn't work -- constructors are special.

In other words, a constructor could be seen as a static (Shared in Visual Basic) method returning an object of a specific type (and some languages actually implement it this way):

Dim myS = New S()      ' can be seen as syntactic sugar for 
                       ' something like myS = S.CreateNew()
like image 35
Heinzi Avatar answered Oct 01 '22 19:10

Heinzi


In the constructor you should only construct the elements for that class, not its base class. By calling the contructor of the base class those elements will be constructed.

like image 22
Michel Keijzers Avatar answered Oct 01 '22 19:10

Michel Keijzers