Class Inheritance

G

Guest

Hi,

I have Class A, and I would like my Class B to inherit from Class A because they are conceptually common classes. My problem is that Class A has a property that I don't want Class B to have. Is there any way to hide, or not inherit, properties of the parent class? Btw, I'm coding in C#, in case that makes a difference.

My intuition says this isn't possible (and probably not desireable from the clarity point of view), but I'm not sure.

Many thanks!

ps. Here's some pseudo-code:

public class MyClassA
{
public MyClassA();

public PropX { get; set; }
public PropY { get; set; }
public PropZ { get; set; }
}

public class MyClassB : MyClassA
{
public MyClassB();

public PropW { get; set; }
public PropQ { get; set; }

// but I don't want this class to inherit PropZ from MyClassA
}
 
R

Richard A. Lowe

You can't really hide it - you can only hide it in the sense of replacing it
(either by overriding or 'hiding' via the new keyword) by adding a new
member that doesn't do anything.

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
ubergoober said:
Hi,

I have Class A, and I would like my Class B to inherit from Class A
because they are conceptually common classes. My problem is that Class A
has a property that I don't want Class B to have. Is there any way to hide,
or not inherit, properties of the parent class? Btw, I'm coding in C#, in
case that makes a difference.
My intuition says this isn't possible (and probably not desireable from
the clarity point of view), but I'm not sure.
 
C

Cindy Winegarden

Hi UberGoober,

Here's an interesting discussion of exactly this issue:
http://tinyurl.com/26vdb. One suggestion was to have a parent class that
Class A and Class B both inherit from and have the property in question be
part of Class A but not of Class B.

--
Cindy Winegarden MCSD, Microsoft Visual FoxPro MVP
(e-mail address removed) www.cindywinegarden.com


ubergoober said:
Hi,

I have Class A, and I would like my Class B to inherit from Class A
because they are conceptually common classes. My problem is that Class A
has a property that I don't want Class B to have. Is there any way to hide,
or not inherit, properties of the parent class? Btw, I'm coding in C#, in
case that makes a difference........
 
J

Jon Skeet [C# MVP]

ubergoober said:
I have Class A, and I would like my Class B to inherit from Class A
because they are conceptually common classes. My problem is that
Class A has a property that I don't want Class B to have.

In that case, class B *shouldn't* inherit from class A. Have a look for
Liskov's Substitutability Principle for more information. Possible
workarounds:

o Make class B contain a reference to an instance of class A, and proxy
appropriate methods/properties
o Derive both class A and class B from another (possibly abstract)
class.
 

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