473,398 Members | 2,404 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,398 software developers and data experts.

Class Inheritance

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
}
Jul 21 '05 #1
3 1266
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" <ub********@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
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
}

Jul 21 '05 #2
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
ci**************@mvps.org www.cindywinegarden.com
"ubergoober" <ub********@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
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........
Jul 21 '05 #3
ubergoober <ub********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
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

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...
6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
22
by: Marcin Vorbrodt | last post by:
Taken out of C++ In a Nutshell book... Example 7-18: Using an abstract classes as interface specification. struct Runnable { virtual void run() = 0; }; struct Hashable { virtual size_t...
9
by: mead | last post by:
What kind of classes is qualified as "concrete classes"? When should a member function in a class defined as "pure virtual" and when as "virtual"? Thanks!
4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
11
by: Darren.Ratcliffe | last post by:
Hi guys Posted what was probably a rather confusing post about this the other day, so I am going to have another go and see if I can make more sense. This sis purely a I've got a base class...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
3
by: Jam | last post by:
Hello All, Your comment is needed on following subject,i define the Class and Inheritance as following,what do you think? Class: Class is a set of objects which shares common states and...
3
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.