Sub class constructor

J

JC Voon

Hi:

The parent class have 3 version of constructor:

public class Parent
public sub New()
end sub

public sub New(i as integer)
myclass.New()
...
end sub

public sub New(i as integer, s as string)
myclass.New()
...
end sub
end class


public class Child
inherits Parent

end class


When i try to create the Child object, i can only use the New() as a
constructor, the others 2 constructor is not accessable, what am i
missing ?

Thanks
JCVoon
 
M

MN

If you want to use parent constructors when instanciating your child class,
you need to implement the 3 constructors in your child class and call parent
constructor from it;

public class Parent
public sub New()
MyBase.New
...
end sub

public sub New(i as integer)
MyBase.New(i)
...
end sub

public sub New(i as integer, s as string)
MyBase.New(i, s)
...
end sub

...
end class


Note that when you instantiate a class, you are calling the class
constructors not parent class constructor;

Regards.
 
H

Herfried K. Wagner [MVP]

JC Voon said:
The parent class have 3 version of constructor:

public class Parent
public sub New()
end sub

public sub New(i as integer)
myclass.New()
...
end sub

public sub New(i as integer, s as string)
myclass.New()
...
end sub
end class


public class Child
inherits Parent

end class


When i try to create the Child object, i can only use the New() as a
constructor, the others 2 constructor is not accessable, what am i
missing ?

Constructors are not inherited by the base class. You'll have to add the
constructors to the derived class too, but you can simply call the base
class' ctor there:

\\\
Public Class Base
Public Sub New(ByVal i As Integer, ByVal s As String)
...
End Sub
End Class

Public Class Derived
Inherits Base

Public Sub New(ByVal i As Integer, ByVal s As String)
MyBase.New(i, s)
End Sub
End Class
///
 
P

Phill. W

Herfried K. Wagner said:
Constructors are not inherited by the base class. You'll have to add the
constructors to the derived class too

Herfried,
<question type='probably silly'>

Perfectly understandable and I've done it myself often enough, but
doesn't this go against the "Rules" of Polymorphism?

All /other/ methods are inherited, why /not/ the Constructors as well?

TIA,
Phill W.
 
H

Herfried K. Wagner [MVP]

Phill. W said:
<question type='probably silly'>

Perfectly understandable and I've done it myself often enough, but
doesn't this go against the "Rules" of Polymorphism?

All /other/ methods are inherited, why /not/ the Constructors as well?

A subclass is not necessarily able to be created with the data that is
passed to its base class only. Maybe additional data needs to be passed to
the class' constructor. In addition to that, you always call a construtor
on a specific type, so there is no real polymorphism.
 
J

JC Voon

MN, Herfried K.Wagner :

Thanks for the reply.
I missed the inherited and overridable constructor in Delphi...:(
Does it apply to C# too ?

Thanks
JCVoon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top