Matthew Brzezinski
Matthew Brzezinski

Reputation: 1794

Why is it a poor approach to build a class by publicly inheriting another class?

I'm studying for my OOP final, and came across a question that has me a bit stumped. The question is "explain why building a Stack class by publicly inheriting a List class is a poor approach. Describe a better solution."

I'm not sure if my answer is right, but is it because of "publicly inheriting..."? And that it would be better to inherit privately instead so that way no class other than Stack would know of the inheritance?

Upvotes: 5

Views: 123

Answers (7)

Ana M
Ana M

Reputation: 467

It is not always true that you should not publicly inherit from another class. You should certainly not do so if the base class exposes functionality that your inherited class should not provide. I.e., a class that inherits from another is a specialized type of the base class – which means that all of the base class’ functionality should apply to the inherited class. The inherited class should add additional functionality, or or provide a specialized/custom form of the base class’ functionality.

In the case of a list vs. a stack, the stack should NOT expose all the functionality that a list provides.

Specifically: A stack manages a collection of objects by allowing consumers to insert objects into the collection, and remove the last object inserted into the collection. (LIFO). Typical interface methods for a stack are “push” and “pop”

A list, on the other hand does not have the same limitations. It alows access to its objects in any order – often by using an index.

If you publicly derive a stack from a list, a caller that has an instance of your stack class will be able to use the base class’ public methods – which would include functionality to access any member of its collection objects, in any order. Your stack is no longer acting as a stack!!

Upvotes: 1

Neil Kirk
Neil Kirk

Reputation: 21803

For example, you can insert in the middle of a list, but you cannot insert in the middle of a stack, but inheriting a stack from a list implies that you can. If anything, list should inherit from stack (although I wouldn't do that in practice either)

Upvotes: 1

4pie0
4pie0

Reputation: 29744

Public inheritance

class A : public B

is used to denote that class A is some kind of class B, it extends this class. Since Stack is not a List

class Stack : public List

is wrong.

And that it would be better to inherit privately(...)?

Better but more correct IMO would be to implement a Stack by a List as a data member or pointer to implementation.

Upvotes: 1

Stas
Stas

Reputation: 11771

This is because public inheritance affects interface. If Stack publicly inherits List, it will include List interface.

Yes, private inheritance is better approach there. Personally, I prefer using aggregation in such cases.

Upvotes: 1

Matías Fidemraizer
Matías Fidemraizer

Reputation: 64943

Maybe it's about not using inheritance and take advantage of composition instead.

Stack has an internal list instead of Stack is a list (which is false: stack is semantically different than a list).

Upvotes: 2

Cubic
Cubic

Reputation: 15693

Private inheritance is also a bad idea here. The key point is really that a Stack isn't a List. While you could have a list implementation that only supports stack like operations this doesn't follow from the definition of a list.

In general you should always prefer composition over inheritance. Inheritance should, ideally, only be used to introduce runtime polymorphism. (Inheritance can sometimes be used for convenience, but you should limit this as a much as possible as it creates possibly unnecessary coupling to the base class).

Upvotes: 2

templatetypedef
templatetypedef

Reputation: 373082

If a derived class publicly inherits from a base class, the derived class is an instance of the base class, potentially with some extra functionality or overridden functionality. In the case of a stack and a list, a stack isn't a list - it logically doesn't perform list operations like searching, concatenating, reversing, etc. - so inheriting from list would not be a good idea.

You could use private inheritance here, but that's not a particularly elegant solution. Instead, you could just implement stack by having it hold a list as a private data member. This is the reason why you often hear "favor composition (having private data members) over inheritance."

Hope this helps!

Upvotes: 6

Related Questions