473,508 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1712
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
1900
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
1803
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
1411
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
1562
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
1801
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
1102
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
1866
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
2117
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
2534
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
4252
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
7123
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
7383
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
5627
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,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1557
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.