473,386 Members | 1,758 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,386 software developers and data experts.

Inherited Constructors

SLE
Hi there,

I know constructors are not inherited from the base class, not in VB.NET nor
C# (nor Java I suppose). I never wondered,but reflecting on the reason why,
I cannot find a solid answer.

Is the reason technical (compiler or CLR limitation) or logical (OOP best
practices)?

Any feedback would be greatly appreciated.
Thanks,

--
SLE
Jul 21 '05 #1
3 1742
SLE <in**@NOSPAM.dataworx.be> wrote:
I know constructors are not inherited from the base class, not in VB.NET nor
C# (nor Java I suppose). I never wondered,but reflecting on the reason why,
I cannot find a solid answer.

Is the reason technical (compiler or CLR limitation) or logical (OOP best
practices)?


It's not always appropriate for a derived class to be able to be
constructed with only the parameters the base class requires. For
instance, System.Object has a parameterless constructor. If
constructors were inherited, *every* class would have to have a
parameterless constructor. What would a parameterless constructor for
FileStream mean?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
SLE
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
SLE <in**@NOSPAM.dataworx.be> wrote:
I know constructors are not inherited from the base class, not in VB.NET nor C# (nor Java I suppose). I never wondered,but reflecting on the reason why, I cannot find a solid answer.

Is the reason technical (compiler or CLR limitation) or logical (OOP best practices)?


It's not always appropriate for a derived class to be able to be
constructed with only the parameters the base class requires. For
instance, System.Object has a parameterless constructor. If
constructors were inherited, *every* class would have to have a
parameterless constructor. What would a parameterless constructor for
FileStream mean?


Well, suppose parameterless constructors actually were inherited, a
parameterless ctor for FileStream could mean nothing :) untill some
Init(params) method was called. Without the latter, other FS methods should
throw exceptions. In other words, *if* ctors were inherited, the FileStream
class would have had a different implementation.

You're right but the case you mention could have been solved at design time.
I simply like to know why the .NET team made the decision not to inherit
constructors (as opposed to e.g. Delphi).

I see some benefits in derived constructors, such as in this simplified
example:

Public Class Person
Private _name As String
Private _age As String

Public Sub New()
End Sub

Public Sub New(ByVal name As String, ByVal age As Integer)
_name = name
_age = age
End Sub
End Class

Public Class Woman
Inherits Person
End Class
1) I cannot instantiate a Woman object with Dim w as New Woman("Justine",
2) - the only way is to explicitly add a contructor with the same signature
to the Woman class:

Public Sub New(ByVal name As String, ByVal age As Integer)
MyBase.New(name, age)
End Sub

One should replicate each parametrized ctor of the base class to all derived
classes ... Good thing someone invented copy/paste but this seems more like
writing a book than coding to me ;)

2) Now, suppose a parameter type should change (we do not have something
like generics yet), or a new param should be added (for instance, data of
birth) on the base class, I need the change the constructor of *all* derived
classes.
--
SLE
Jul 21 '05 #3
SLE <in**@NOSPAM.dataworx.be> wrote:
It's not always appropriate for a derived class to be able to be
constructed with only the parameters the base class requires. For
instance, System.Object has a parameterless constructor. If
constructors were inherited, *every* class would have to have a
parameterless constructor. What would a parameterless constructor for
FileStream mean?
Well, suppose parameterless constructors actually were inherited, a
parameterless ctor for FileStream could mean nothing :) untill some
Init(params) method was called.


So instead of having a type which is guaranteed to be useful
immediately, you end up with an error-prone two-phase initialization,
just so that you can inherit a constructor you don't want in the first
place. There's no way that's a good idea.
Without the latter, other FS methods should throw exceptions.
So instead of having to add a few constructors occasionally, you have
to add checks to every single method in the class. Where's the benefit
exactly?

<snip>
1) I cannot instantiate a Woman object with Dim w as New Woman("Justine",
2) - the only way is to explicitly add a contructor with the same signature
to the Woman class:

Public Sub New(ByVal name As String, ByVal age As Integer)
MyBase.New(name, age)
End Sub
And what's the problem with that?
One should replicate each parametrized ctor of the base class to all derived
classes ... Good thing someone invented copy/paste but this seems more like
writing a book than coding to me ;)
And yet you're advocating adding a check to every single method in
every class which doesn't want to allow a parameterless constructor?
That way madness lies.

Admittedly I wish the IDE made it easy to create "stub" constructors
which just called the base ones - Eclipse makes this very easy in Java,
for instance. I haven't checked whether that facility is in Whidbey or
not.
2) Now, suppose a parameter type should change (we do not have something
like generics yet), or a new param should be added (for instance, data of
birth) on the base class, I need the change the constructor of *all* derived
classes.


Absolutely - thank goodness. The type should know the exact set of ways
in which it can be constructed. If it's going to go through some
otherwise unknown channel, it could bypass your initialisation code.
The whole thing becomes *far* more flaky when it can bypass all your
provided constructors just because someone added a constructor to the
base class.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4

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

Similar topics

3
by: Alexander Muylaert | last post by:
Hi Is their a way to keep constructors visible in inherited classes? public class X{ public x(int A){} } public class Y : X{};
2
by: PIEBALD | last post by:
OK, here's my latest workaround for the lack of inherited constructors... It requires that the class have a parameterless (default) constructor and a public virtual "Initialize" method (which...
3
by: M O J O | last post by:
Hi, I've created a MasterForm which all my forms in my project must derive from. In my MasterForm, I've overloaded the New event with this code: Public Sub New(ByVal SomeText As String)...
3
by: SLE | last post by:
Hi there, I know constructors are not inherited from the base class, not in VB.NET nor C# (nor Java I suppose). I never wondered,but reflecting on the reason why, I cannot find a solid answer. ...
3
by: Wayne Brantley | last post by:
VS2005 RTM Create a web user control to use as a base class for other web user controls. Now, create a new web user control, change the class it inherits from to your base class and compile....
12
by: Oleg Subachev | last post by:
I am moving from Delphi to C# and hve encountered the problem: I have the following classes and form Load event handler: public class class1 { public string S; public class1( string aS ) {...
12
by: Mike - EMAIL IGNORED | last post by:
Within class MyClass, I can think of two ways to tell if MyClass is inherited in a particular use: 1. pass an appropriate bool in the ctor args; 2. use a virtual method that returns, for...
2
by: Søren M. Olesen | last post by:
Hi How do I create an instance of an object with an inherited cunstructor?? In the below example, I'm able to create an instance of MyClass2 using the forst two lines of code, however if I try...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.