INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Question about inheritance

Question about inheritance

(OP)
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?
 

RE: Question about inheritance

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: Database Connection Management
FAQ732-7259: Keeping the UI responsive

RE: Question about inheritance

>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.

RE: Question about inheritance

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close