473,668 Members | 2,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1765
SLE <in**@NOSPAM.da taworx.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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
SLE <in**@NOSPAM.da taworx.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.da taworx.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.co m>
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
1427
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
1481
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 returns its "this"). An instance (in this case a Number) is constructed and initialized like so: Number n = ((Number)((Number) typeof(Number).GetConstructor(System.Type.EmptyTypes).Invoke(new object{})).Initialize(1)) ;
3
1245
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) MyBase.New()
3
248
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. Is the reason technical (compiler or CLR limitation) or logical (OOP best practices)? Any feedback would be greatly appreciated.
3
5470
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. (You must have a <% Register %> so it will see it) You will get TWO warnings per class like:
12
1768
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 ) { S = aS;
12
1505
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 example, a siring containing the class name. Is there a better way?
2
1566
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 using the next two lines of code it fails.
14
2627
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 Language, but there isn't a detail and complete description on all the class members, aren't they important to class composing? Could you explain the special class behavior in detail? Thank you very much.
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8378
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8653
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2786
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.