views:

270

answers:

3

I've been teaching OOP and was trying to convey to my students the important difference between inheritance and the creation of a subtype relation between two types. For example, in C++, I could use private inheritance to ensure that nobody outside sees the subtyping relation.

However, while I can think of a lot of situations where I wouldn't want to create a subtyping relation (e.g., implementing a Stack via a doubly-linked list class), I can't really think of good design examples where I would actually choose to follow inheritance without creating a public subtying relation (rather than, say, use aggregation).

Any ideas of good examples?

A: 

Code reuse without subtyping is done using delegation.

Delegation is inheritance.

ddaa
+3  A: 

As a Python programmer, let me say that these are matters that are way, way too subtle. They appear to be purely C++-isms.

In Python, we have inheritance, which creates proper subtypes.

And we have composition using simple attributes, or of the available collections.

This business of private inheritance being a kind of composition seems to be simply confusing. And largely useless.

The C++ examples show private inheritance to create composition with a common interface. In Java, we'd have a common interface. In Python, we have duck typing and don't need the formalisms.

I don't think there are good examples -- I think the concept is too subtle to be of any real value.

S.Lott
I agree completely. I am not convinced private inheritance is actually useful.
Pramod
A: 

In javascript you have inheritance by means of prototype. Which I wouldn't call a subtype. As there are no classes in javascript objects inherit from their prototype, but at the same time objects with the same prototype are independent ofr each other and can evolve to very different types of objects.

John Nilsson