Question about inheritance and constructors.

  • Thread starter Thread starter Kevin Buchan
  • Start date Start date
K

Kevin Buchan

I searched the news group and could not find an answer to this question, so
I'll go ahead and post it.

Let's say I have a class A with a couple different constructors... nothin'
special.

Now, let's say that I have class B that inherits from A. B automagically
gets all of the properties and methods of A, but I cannot leverage any of
A's contructors without redefining them on B and delegating. I'm sure that
there is a very good reason for this, but I find it a bit frustrating.

Is there, perhaps, some syntax shotcut that I'm not familiar with for
leveraging the A constructors without having to mirror them on class B?

Thanks so much.

-Kevin Buchan
 
IIRC, Constructors are not inherited. Your derived classes must call
the constructors of the base class using the MyBase keyword:

Public Sub New()
'Call base class constructor
MyBase.New()
End Sub
 
IIRC, Constructors are not inherited. Your derived classes must call
the constructors of the base class using the MyBase keyword:

Public Sub New()
'Call base class constructor
MyBase.New()
End Sub


Yup, that's what I've been doing, but when your base class has three
constructors, it's a bit of an annoyance to replicate and delegate in
multiple subclasses.
 
Jason said:
Yup, that's what I've been doing, but when your base class has three
constructors, it's a bit of an annoyance to replicate and delegate in
multiple subclasses.

I suppose it would be nice if there were some kind of Wizard that
automatically brought in the source of a given base class method, where
available. An idea for a VS add-in maybe.

But I'm sure you can see why constructors aren't *automatically*
inherited - a derived class has -something- more than the base class,
so will almost always want constructors that do more than the base
class constructors. Of all the behaviours of a derived class as
compared to its parent, instantiation is surely high on the list of
behaviours that will differ.
 
Kevin Buchan said:
Let's say I have a class A with a couple different constructors... nothin'
special.

Now, let's say that I have class B that inherits from A. B automagically
gets all of the properties and methods of A, but I cannot leverage any of
A's contructors without redefining them on B and delegating. I'm sure
that there is a very good reason for this, but I find it a bit
frustrating.

Is there, perhaps, some syntax shotcut that I'm not familiar with for
leveraging the A constructors without having to mirror them on class B?

As Chris said, constructors are not inherited. The reason for that is that
data passed to one of the base class' constructor is not necessarily
sufficient to construct an object of the base class. Example:

\\\
Public Class DownloadClient
Public Sub New(ByVal UserName As String)
...
End Sub
...
End Class
///

Imagine you want to create a subclass of this class that will download data
using username /and/ password, and thus a password would be required for
instantiation and initialization of the object. If your derived class would
inherit the constructor of 'DownloadClient', this constructor would not be
sufficient and thus should not exist. In addition to that, constructors
cannot be accessed polymorphically and thus inheritance would not make much
sense from this perspective too.
 
Kevin,

Does this sample I made for you makes it clear?.

\\\\
Public Class Class1
Public Shared Sub Main()
Dim sample As New my("Kevin", "Dunaway")
MessageBox.Show(sample.myfirstname & " " _
& sample.hislastname)
End Sub
End Class
Public Class his
Public hislastname As String
Public Sub New(ByVal lastname As String)
hislastname = lastname
End Sub
End Class
Public Class my
Inherits his
Public myfirstname As String
Public Sub New(ByVal firstname As String, _
ByVal lastname As String)
MyBase.New(lastname)
myfirstname = firstname
End Sub
End Class
///
I hope this helps a little bit.

Cor
 
Jason Kendall said:
Yup, that's what I've been doing, but when your base class has three
constructors, it's a bit of an annoyance to replicate and delegate in
multiple subclasses.

it needs to be this way since you can cast your object back to its base
type -- the call to MyBase.New(...) is what sets this up.
 
I suppose it would be nice if there were some kind of Wizard that
automatically brought in the source of a given base class method, where
available. An idea for a VS add-in maybe.

But I'm sure you can see why constructors aren't *automatically*
inherited - a derived class has -something- more than the base class,
so will almost always want constructors that do more than the base
class constructors. Of all the behaviours of a derived class as
compared to its parent, instantiation is surely high on the list of
behaviours that will differ.


Of course you're right, but it would seem like there'd be a keyword or
an attribute or something that woudl let me do this.

Now that I think about it, though, that would require me, as a base
class developer, to force my constructors onto users of my class.

OK, I'm sold. Thanks for the reply, Larry.

-Kevin Buchan
 
it needs to be this way since you can cast your object back to its base
type -- the call to MyBase.New(...) is what sets this up.

You're saying to call MyBase.New(XX) in the subclass's constructors?
Yeah, that's what I'm currently doing.

Or are you saying that I can do something like:
Dim AnInstance of MySubclass = CType(New MyBase(X),
MySubclass)

That would be really nice if it'd work. I'll have to try it, but I'm
feelin' like I'd get a type conversion. It should work converting the
sub to the base, but not this way, I think. I'll have to try it.

Thanks.

-Kevin
 
As Chris said, constructors are not inherited. The reason for that is that
data passed to one of the base class' constructor is not necessarily
sufficient to construct an object of the base class. Example:

\\\
Public Class DownloadClient
Public Sub New(ByVal UserName As String)
...
End Sub
...
End Class
///

Imagine you want to create a subclass of this class that will download data
using username /and/ password, and thus a password would be required for
instantiation and initialization of the object. If your derived class would
inherit the constructor of 'DownloadClient', this constructor would not be
sufficient and thus should not exist. In addition to that, constructors
cannot be accessed polymorphically and thus inheritance would not make much
sense from this perspective too.

OK, in the case you described, there would be no benefit to getting
the constructors through inheritence. However, if you have a base
class that holds all the data and subclasses that simply provide
different ways of using the data, then the constructors would be
exactly the same and supremely useful to have automatically propagated
to all subclasses.

I can see very strong arguments for not allowing this, though, and
it's not exactly rocket surgery to reproduce the constructors I need
in my subclasses.

Thanks so much for the reply, Herfried.
 

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

Back
Top