TheGarden
TheGarden

Reputation: 191

Class Inheritance Theoretical Insight

I was asked recently the following question and wasn't sure how well I did answering it so I wanted to see what people's thoughts were on the question:

If you were dealing with a class of apples and a class of bushels. What class would inherit from the other and why?

Let me know your thoughts, thanks.

Upvotes: 1

Views: 28

Answers (2)

Blckknght
Blckknght

Reputation: 104712

I don't think either class should derived from the other. Apples and bushel baskets are fundamentally different things. A bushel may contain apples, but there's no IS-A relationship which would suggest inheritance.

Probably the Apple class should derive from a more abstract Fruit class, while Bushel might inherit from some more abstract Container class. You could then specify that a Container contains zero or more instances of Fruit.

Upvotes: 1

Peter Gluck
Peter Gluck

Reputation: 8236

Neither. Apples are not bushels and bushels are not apples. Bushels contain apples, so the relationship is one of containment, not inheritance.

Upvotes: 1

Related Questions