Inheritance - issue with Constructors

  • Thread starter Thread starter Doug
  • Start date Start date
D

Doug

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?
 
Doug said:
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.
 
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.
--
 
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.
 
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.
 
Doug said:
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.
 
Back
Top