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.
Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
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.
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.
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.
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.
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With