about inheritance

T

tony

Hello!

Assume I have a base class called Base and a derived class called Sub.

Assume all the property for the Base class can't fit exactly for the Sub
class.
I mean some of the property for the Base class is wrong for the Sub class is
it then allowed
to inherit from class Base.

The main issue when using inheritance is that Sub is a Base.

//Tony
 
J

Jon Skeet [C# MVP]

tony said:
Assume I have a base class called Base and a derived class called Sub.

Assume all the property for the Base class can't fit exactly for the Sub
class.
I mean some of the property for the Base class is wrong for the Sub class is
it then allowed to inherit from class Base.

The main issue when using inheritance is that Sub is a Base.

In that case, you shouldn't be using inheritance. Inheritance is often
overused. It can be incredibly useful at the right time, but often
people use inheritance when they should be using
composition/aggregation.

See
http://msmvps.com/jon.skeet/archive/2006/03/04/inheritancetax.aspx
 
C

Christopher Ireland

| Assume I have a base class called Base and a derived class called Sub.
|
| Assume all the property for the Base class can't fit exactly for the Sub
| class.
| I mean some of the property for the Base class is wrong for the Sub class
is
| it then allowed
| to inherit from class Base.
|
| The main issue when using inheritance is that Sub is a Base.

How about inheriting from an interface: you have an interface that contains
what you want to share between Base and Sub and then have both those classes
inherit from it and implement it, eg

class Base : ICommon
class Sub : ICommon

In this way Sub and Base are ICommon.

Chris.
 
G

Guest

Assume all the property for the Base class can't fit exactly for the Sub
class.
I mean some of the property for the Base class is wrong for the Sub class is
it then allowed
to inherit from class Base.

These other guys are correct in that inheritance may not be the way to go
here. But if you absolutely must inherit Sub from Base, you can always hide
the properties in Base that you don't want to appear in Sub with the "new"
keyword then mark them private.

For example, assume you don't want the "BaseProp" property to be visible in
the Sub class:

public class Base
{
private int m_BaseProp;
public int BaseProp
{
get
{
return this.m_BaseProp;
}
set
{
this.m_BaseProp = value;
}
}
}
public class Sub : Base
{
new private int BaseProp
{
get
{
return base.BaseProp;
}
set
{
base.BaseProp = value;
}
}
}
 
C

Cor Ligthert [MVP]

Tony,

You see this done in much Net classes. Take by instance the Controls.
The picturebox has no background and still the property exist.
It is just overriden and does nothing.
(Assuming that you mean something the same)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top