473,665 Members | 2,820 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 1728
tony <jo************ *****@telia.com wrote:
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.co m>
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.com wrote in message
news:%2******** ********@TK2MSF TNGP02.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.com schreef in bericht
news:%2******** ********@TK2MSF TNGP02.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
1907
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 the following: One has several (metaclass) modules that implements an interesting feature. Lets say, I have a metaclass L, that adds logging capabilities to all method calls and another one, called P, that creates properties on the fly.
17
1817
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 operator (if any). So, an expression might be a primary (which, in turn, might be a variable or a constant), a unary expression (i.e. the result of applying a unary operator to an expression), a binary expression, and so on. If I were solving...
3
1414
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
1571
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 level. There is a class TeachingAssistent that use multiple inheritance from both Student and Employee. There is a method named getName is class Person.
1
1810
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 know that an abstract class doesn't have any implementaion even if a default implementatiion can be supplied for pure virtual methods. What does it actually mean with saying that an Abstract superclasses define a behavioral pattern?
3
1105
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 classB: class classA(){ ......
18
1900
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 requests at the same time. The first one stops execution as the second continues. If I place either an alert between the two requests or run the second through a setTimeout of only 1 millisecond, they both work. You can see a working example here:...
14
2138
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 and holds everything a unit in my game is supposed to do. What are some some cures for this kind of large object or are they OK because they represent one thing. If not what are better ways to design objects that behave the same way. Would it be...
3
2548
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 used, then "the responsibility for initializing a virtual base is borne by the most derived class in the hierarchy". What does it mean? Initializing base class is usually done automatically by the compiler, but a derived class can invoke the base...
8
4262
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 raise an event how do I create an handler to catch these raised events. //Tony
0
8438
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8779
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6187
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.