473,396 Members | 1,916 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,396 software developers and data experts.

about inheritance

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
Sep 8 '06 #1
4 1704
tony <jo*****************@telia.comwrote:
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/...itancetax.aspx

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 8 '06 #2
"tony" <jo*****************@telia.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
| 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.
Sep 8 '06 #3
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;
}
}
}

--
Timm Martin
Mini-Tools
..NET Components and Windows Software
http://www.mini-tools.com

Sep 8 '06 #4
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

"tony" <jo*****************@telia.comschreef in bericht
news:%2****************@TK2MSFTNGP02.phx.gbl...
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


Sep 9 '06 #5

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

Similar topics

2
by: Stephan Diehl | last post by:
I have a question about metaclasses: How would be the best way to "merge" different metaclasses? Or, more precisely, what is the best way to merge metaclass functionality? The idea is basicly...
17
by: Andrew Koenig | last post by:
Suppose I want to define a class hierarchy that represents expressions, for use in a compiler or something similar. We might imagine various kinds of expressions, classified by their top-level...
3
by: arserlom | last post by:
Hello I have a question about inheritance in Python. I'd like to do something like this: class cl1: def __init__(self): self.a = 1 class cl2(cl1): def __init__(self): self.b = 2
2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
1
by: Tony Johansson | last post by:
Hello Experts! I have some questions about inheritance that I want to have an answer to. It says "Abstract superclasses define a behavioral pattern without specifying the implementation" I...
3
by: Quentin Huo | last post by:
Hi: C# doesn't support multiple inheritance but it supports interface.But I think these ways are different. For example, C++ supports multiple inheritance. I have two classess classA and...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
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...
8
by: Tony Johansson | last post by:
Hello! I wonder can somebody explain when is it suitable to use these methods OnKeyUp, OnKeyDown and OnKeyPress because these raise an event. These are located in class UserControl. If these...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.