473,387 Members | 1,812 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.

Inheritance - issue with Constructors

I have a public abstract class that I want to inherit from in two other
classes. My public abstract one has a constructor with several
parameters. For some reason I cannot get to that constructor unless I
create a constructor in my two other classes like this:

public ClassName(string szParameter1, string szParameter2, string
szParameter3) : base(szParameter1, szParameter2, szParameter3)
{
}

This doesn't make sense to me. The reason I did this was to save on
code and now I've essentially got my parameters listed 2 times more
than I thought I'd need for each class that inherits from the abstract
one.

Is there a better way to do this?

Nov 22 '05 #1
12 1582
This doesn't make sense to me.
Yes it does, constructors aren't inherited.

Is there a better way to do this?


No you're doing it the right way.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #2
This doesn't make sense to me.
Yes it does, constructors aren't inherited.

Is there a better way to do this?


No you're doing it the right way.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 22 '05 #3

"Doug" <dn******@dtgnet.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
I have a public abstract class that I want to inherit from in two other
classes. My public abstract one has a constructor with several
parameters. For some reason I cannot get to that constructor unless I
create a constructor in my two other classes like this:

public ClassName(string szParameter1, string szParameter2, string
szParameter3) : base(szParameter1, szParameter2, szParameter3)
{
}

This doesn't make sense to me. The reason I did this was to save on
code and now I've essentially got my parameters listed 2 times more
than I thought I'd need for each class that inherits from the abstract
one.

Is there a better way to do this?

First, read Mattias' answer. Similarly, destructors are not inherited.
Second, if by "better" you mean simpler, you can consider defining and using
a default constructor in the base class, thus avoiding the:

: base(szParameter1, szParameter2, szParameter3)

But, if the derived class constructor's parameters are needed as arguments
to the base class constructor, that won't work for you.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 22 '05 #4

"Doug" <dn******@dtgnet.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
I have a public abstract class that I want to inherit from in two other
classes. My public abstract one has a constructor with several
parameters. For some reason I cannot get to that constructor unless I
create a constructor in my two other classes like this:

public ClassName(string szParameter1, string szParameter2, string
szParameter3) : base(szParameter1, szParameter2, szParameter3)
{
}

This doesn't make sense to me. The reason I did this was to save on
code and now I've essentially got my parameters listed 2 times more
than I thought I'd need for each class that inherits from the abstract
one.

Is there a better way to do this?

First, read Mattias' answer. Similarly, destructors are not inherited.
Second, if by "better" you mean simpler, you can consider defining and using
a default constructor in the base class, thus avoiding the:

: base(szParameter1, szParameter2, szParameter3)

But, if the derived class constructor's parameters are needed as arguments
to the base class constructor, that won't work for you.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Nov 22 '05 #5
Normally, when folks say "This doesn't make sense to me", that tells me that
you expected one thing, but the framework gave you another thing. Can you
tell us what you expected would happen?

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Nov 22 '05 #6
Thanks for both of your responses. Unfortunately, I do need a
constructor with parameters in the base class because both of my
inherited ones need that same constructor. So I'll have to go with
what I'm doing. Thanks again.

Nov 22 '05 #7
Normally, when folks say "This doesn't make sense to me", that tells me that
you expected one thing, but the framework gave you another thing. Can you
tell us what you expected would happen?

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Nov 22 '05 #8
Thanks for both of your responses. Unfortunately, I do need a
constructor with parameters in the base class because both of my
inherited ones need that same constructor. So I'll have to go with
what I'm doing. Thanks again.

Nov 22 '05 #9
I didn't realize Constructors were not inherited. I thought that by
having an abstract class that two other classes inherited from that I
could essentially have this:

public abstract class AbstractClass
{
decimal m_m1;
decimal m_m2;
decimal m_m3;

public AbstractClass(decimal m1, decimal m2, decimal m3)
{
m_m1 = m1;
m_m2 = m2;
m_m3 = m3;
}
}

public class InheritedClass1 : AbstractClass
{
}

public class InheritedClass2 : AbstractClass
{
}

And then I could call one of the Inherited Classes like this:

InheritedClass1 oNewClass = new InheritedClass1(m1, m2, m3);

and that by doing this, that this line would go to the Constructor in
AbstractClass. This saves a lot of code for me. Instead I have to do
this for my inherited classes:

public class InheritedClass1 : AbstractClass
{
public InheritedClass1 (decimal m1, decimal m2, decimal m3) :
base (m1, m2, m3)
{
}
}
public class InheritedClass2 : AbstractClass
{
public InheritedClass2 (decimal m1, decimal m2, decimal m3) :
base (m1, m2, m3)
{
}
}

It still saves some code, but it seems like some excess to me that I
have to have a constructor in my Inherited classes and that I have to
repeate the parameters in the base entry as well. But I think I know
why now.

Nov 22 '05 #10
I didn't realize Constructors were not inherited. I thought that by
having an abstract class that two other classes inherited from that I
could essentially have this:

public abstract class AbstractClass
{
decimal m_m1;
decimal m_m2;
decimal m_m3;

public AbstractClass(decimal m1, decimal m2, decimal m3)
{
m_m1 = m1;
m_m2 = m2;
m_m3 = m3;
}
}

public class InheritedClass1 : AbstractClass
{
}

public class InheritedClass2 : AbstractClass
{
}

And then I could call one of the Inherited Classes like this:

InheritedClass1 oNewClass = new InheritedClass1(m1, m2, m3);

and that by doing this, that this line would go to the Constructor in
AbstractClass. This saves a lot of code for me. Instead I have to do
this for my inherited classes:

public class InheritedClass1 : AbstractClass
{
public InheritedClass1 (decimal m1, decimal m2, decimal m3) :
base (m1, m2, m3)
{
}
}
public class InheritedClass2 : AbstractClass
{
public InheritedClass2 (decimal m1, decimal m2, decimal m3) :
base (m1, m2, m3)
{
}
}

It still saves some code, but it seems like some excess to me that I
have to have a constructor in my Inherited classes and that I have to
repeate the parameters in the base entry as well. But I think I know
why now.

Nov 22 '05 #11
Doug <dn******@dtgnet.com> wrote:
For some reason I cannot get to that constructor unless I
create a constructor in my two other classes like this:

public ClassName(string szParameter1, string szParameter2, string
szParameter3) : base(szParameter1, szParameter2, szParameter3)
{
}

This doesn't make sense to me.


As has been stated, constructors are not inherited.

From <http://www.yoda.arachsys.com/csharp/constructors.html>:

| Some people have said that they would rather constructors were
| inherited, making the language act as if all derived classes had
| constructors with all the parameter lists from the constructors
| from the base class, and just invoking them with the parameters
| provided. I believe this would be a very bad idea. Take, for
| instance, the FileInfo class. You must logically provide a
| filename when constructing a FileInfo instance, as otherwise
| it won't know what it's meant to be providing information on.
| However, as object has a parameterless constructor, constructors
| being inherited would then mean that FileInfo had a parameterless
| constructor. Some have suggested that this could be fixed by
| allowing you to "override" the parameters you didn't want invoked
| as private, but this goes against the idea that you should never
| be able to override anything to give it more restrictive access,
| and also means that class developers would have to change their
| code every time a new constructor was added to a base class.
Nov 22 '05 #12
Doug <dn******@dtgnet.com> wrote:
For some reason I cannot get to that constructor unless I
create a constructor in my two other classes like this:

public ClassName(string szParameter1, string szParameter2, string
szParameter3) : base(szParameter1, szParameter2, szParameter3)
{
}

This doesn't make sense to me.


As has been stated, constructors are not inherited.

From <http://www.yoda.arachsys.com/csharp/constructors.html>:

| Some people have said that they would rather constructors were
| inherited, making the language act as if all derived classes had
| constructors with all the parameter lists from the constructors
| from the base class, and just invoking them with the parameters
| provided. I believe this would be a very bad idea. Take, for
| instance, the FileInfo class. You must logically provide a
| filename when constructing a FileInfo instance, as otherwise
| it won't know what it's meant to be providing information on.
| However, as object has a parameterless constructor, constructors
| being inherited would then mean that FileInfo had a parameterless
| constructor. Some have suggested that this could be fixed by
| allowing you to "override" the parameters you didn't want invoked
| as private, but this goes against the idea that you should never
| be able to override anything to give it more restrictive access,
| and also means that class developers would have to change their
| code every time a new constructor was added to a base class.
Nov 22 '05 #13

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

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
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; };
6
by: Doug | last post by:
I have a public abstract class that I want to inherit from in two other classes. My public abstract one has a constructor with several parameters. For some reason I cannot get to that constructor...
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...
19
by: Mike Tyka | last post by:
Hello community, i'm fairly new to using the STL but i've been experimenting a bit with it. I tried to derive a new class say MyString from string like so: class MyString: public string{...
3
by: Gianni Mariani | last post by:
In the code below, controller::controller() is never invoked, however, it appears there is no way to make a compile-time rule that this should not happen. The code below seems to make compilers...
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...
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...
2
by: Tim Van Wassenhove | last post by:
Hello, When i read the CLI spec, 8.10.2 Method inheritance i read the following: "A derived object type inherits all of the instance and virtual methods of its base object type. It does not...
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
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?
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:
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.