473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Question about inheritance and constructors.

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
Nov 21 '05 #1
10 1198
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

Nov 21 '05 #2
On 20 Apr 2005 06:32:59 -0700, "Chris Dunaway" <du******@gmail.com>
wrote:
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.
Nov 21 '05 #3

Jason Kendall wrote:
On 20 Apr 2005 06:32:59 -0700, "Chris Dunaway" <du******@gmail.com>
wrote:
IIRC, Constructors are not inherited. Your derived classes must callthe 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.


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.

--
Larry Lard
Replies to group please

Nov 21 '05 #4
"Kevin Buchan" <as**************@hotmail.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
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
Nov 21 '05 #6

"Jason Kendall" <Ja**********@hotmail.com> wrote in message
news:pa********************************@4ax.com...

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.
Nov 21 '05 #7
On 20 Apr 2005 09:00:45 -0700, "Larry Lard" <la*******@hotmail.com>
wrote:

Jason Kendall wrote:
On 20 Apr 2005 06:32:59 -0700, "Chris Dunaway" <du******@gmail.com>
wrote:
>IIRC, Constructors are not inherited. Your derived classes mustcall >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.


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
Nov 21 '05 #8
On Wed, 20 Apr 2005 15:44:14 -0400, "stand__sure"
<st*********@hotmail.com> wrote:

"Jason Kendall" <Ja**********@hotmail.com> wrote in message
news:pa********************************@4ax.com.. .

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.


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
Nov 21 '05 #9
On Wed, 20 Apr 2005 18:16:18 +0200, "Herfried K. Wagner [MVP]"
<hi***************@gmx.at> wrote:
"Kevin Buchan" <as**************@hotmail.com> schrieb:
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.


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.

Nov 21 '05 #10
Thank you very much for the clear description and the code example,
Cor.

-Kevin
On Wed, 20 Apr 2005 18:25:03 +0200, "Cor Ligthert"
<no************@planet.nl> wrote:
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


Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Alexander Stippler | last post by:
I've got an inheritance structure with two coupled "dreaded diamonds" like shown below: A / \ / \ B C \ / \ \ / \ D E \ /
4
by: Jimmy Johns | last post by:
Hi, I have some classes as follows: #include <iostream> using namespace std; class A { public: virtual A* clone() const = 0; };
1
by: Russ Ford | last post by:
Hi all, I'm trying to get inheritance and constructors clear in my head (and in my code). I have the following inheritance situation (all derivations public): A is the base class B is...
4
by: Busin | last post by:
When a child class inherits from a base class, will the child class inherits everything of the base class, including all member variables and functions? Or is such inheritance "selective", like not...
2
by: Jim Owen | last post by:
In the following code, as I understnad it, the resulting display would be "I am B", because I have overridden the constructor in B. However, what if what I really want to do is display "I am A"...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
7
by: Adam Nielsen | last post by:
Hi everyone, I'm having some trouble getting the correct chain of constructors to be called when creating an object at the bottom of a hierarchy. Have a look at the code below - the inheritance...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.