Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Inherit, but not everything...

2 views
Skip to first unread message

Microsoft Communities

unread,
May 9, 2002, 12:13:25 PM5/9/02
to
Hi,

I'm trying to Inherit from a class that is herited from another class...the
final result should be that I get all of the propertyes and methods from
both classes without having to code dupicate properties in the final class.

My problem is that I don't want all of the properties inherited in the new
class, although others will do.

Can someone point me in the right direction真

Cheers
Kieron

kie...@howling.fslife.co.uk


Stewart Saathoff

unread,
May 9, 2002, 4:22:00 PM5/9/02
to
Why not just add both classes to the project....

"Microsoft Communities" <kie...@howling.fslife.co.uk> wrote in message
news:OCISHS39BHA.2092@tkmsftngp05...

Kieron Lanning

unread,
May 9, 2002, 5:39:46 PM5/9/02
to
Sorry, that's not what I meant.

PartClass Top
| \
| MotorClass
BatteryClass
|
|
ChassisClass Bottom

The BatteryClass has two properties, 1) Amps 2) IsSaddleConfiguration

I want the ChassisClass to inherit everything from the BatteryClass
(which has all the functionality of the PartClass), except the Amps
property.

All of the classes are in the same project.

Regards
Kieron

kie...@howling.fslife.co.uk

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jay B. Harlow [MVP - Outlook]

unread,
May 9, 2002, 8:05:26 PM5/9/02
to
Kieron,
Unfortunately. Or is it Fortunately!

That is not supported!

When you say that ChassisClass inherits from BatteryClass, you are saying
that ChassisClass can be used every place a BatteryClass can be used. If you
don't want to ChassisClass to have a Amps property, then it is no longer a
BatteryClass!

What I would suggest is that you introduce a new class that is between
PartClass & BatteryClass, that BatteryClass & ChassisClass both inherit
from... Only BatteryClass would have the Amps property...

Hope this helps
Jay

"Kieron Lanning" <kie...@howling.fslife.co.uk> wrote in message
news:OYu3KI69BHA.2372@tkmsftngp07...

J Quick

unread,
May 11, 2002, 12:26:09 PM5/11/02
to
"Kieron Lanning" <kie...@howling.fslife.co.uk> wrote in message
news:OYu3KI69BHA.2372@tkmsftngp07...

> Sorry, that's not what I meant.
>
> PartClass Top
> | \
> | MotorClass
> BatteryClass
> |
> |
> ChassisClass Bottom
>
> The BatteryClass has two properties, 1) Amps 2) IsSaddleConfiguration
>
> I want the ChassisClass to inherit everything from the BatteryClass
> (which has all the functionality of the PartClass), except the Amps
> property.
>
> All of the classes are in the same project.
>

I suspect that you don't understand that hierarchical inheritance is used only
for a generalization/specialization relationship (when one class is a "kind-of"
another class). It shouldn't be used for an aggregation/composition
relationships (when one object is "part-of" another).

Is a chassis a kind of battery?
Do all batteries have amps?
If the answer to both of these questions is yes, than a chassis therefore MUST
have amps or it CANNOT be a kind of battery. If a chassis is not a kind of
battery, then it shouldn't have an inheritance relationship from the battery
class.

Or, as I suspect, is a battery just a possible component of a chassis, or visa
versa?

You probably also shouldn't have a "IsSaddleConfiguration" property on the
Battery class but instead have a descendant "SaddleConfigurationBattery" or just
"SaddleBattery" class. This is because the meaning of the property name itself
implies that not all batteries are "SaddleConfiguration", and if so, then it
isn't appropriate to have this property on a general "Battery" class. You can
determine whether a battery is SaddleConfiguration without having to have a
meaningless property on all "non-saddle configuration" batteries.

For example, this would be like having a "IsHorseDrawn" property on a vehicle
class, which wouldn't make much sense when inherited by non-horsedrawn vehicles
such as "SubmarineVehicle" or "SpaceVehicle", right?


0 new messages