Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about inheritance

Status
Not open for further replies.

dugogota

Programmer
Oct 20, 2011
1
0
0
US
I'm trying to figure out how to do the following:

Class A has data items:
car
bike
phone
cup

Class B inherits Class A but only the data items car and bike. Data items phone and cup are not available to Class B

Class B adds
tire
handlebars
bell
helmet

Class C inherits from Class B, it also has access to data items car and bike from Class A, tire and handlebars from Class B (but not bell and helmet).

So here is where I'm stuck. It seems like inheritance will allow Class B access to all data items in Class A. That is, nothing can be hidden from Class B unless Class A is private to Class B in which case all data items from Class A are unavailable to Class B.

So, what to do when you want to pass along some access to some data items but not all data items from a super class to a sub class and down another sub class as in the Class A, B, and C example above?
 
from what you describe this these objects are not well suited for inheritance. Also providing the names of the classes, rather than Class "A", "B" & "C" would provide value. the naming is just as important as the functionality.

To start it doesn't make much sense why Class A has the properties Car, Bike, Phone, Cup. even with more information I can't see how these four concepts would be merged into a single object.

Second, the attributes of Class B seem to be elements of a bike. Helmet isn't, that is something a person wears who is riding a bike, but Class B seems like it should be Bike, not have a bike.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
>So, what to do when you want to pass along some access to some data items but not all data items from a super class to a sub class and down another sub class as in the Class A, B, and C example above?

The answer is that you don't. If you have to do this, your objects are not well suited for inheritance as Jason says. If you want to understand inheritance, think about it in terms of generalization and specialization. Generalization looks for behaviors and attributes that objects have in common, and implements them in a base class which the objects inherit. Specialization works in the other direction, thinking about ways that a base class can be applied if functionality is added. Your stuff doesn't appear to lend itself to these thought processes.

I would suggest that you think in terms of aggregation instead. Aggregation is a concept of parts making up a whole, and you gain reuse in that way. It's often helpful to think of it this way: a subclass "is a" base class, an aggregate object "has a" given part. So, a bike "has a" bell, and handlebars. It "is a" vehicle, and in a more specialized sense it "is a" wheeled vehicle, and a human-powered vehicle. I see you thinking here more in terms of "has a" than "is a".

An unforeseen consequence of the information revolution has been the exponential propagation of human error.
 
in order to do this you make phone and cup private to Class A. this will prevent passing of these fields to classes inherited from A.
Same with bell and helmet for class B.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top