What is the flaw with the Stack class?

Stack class in Java violates the Liskov Substitution Principle and has some flaws. java.util.Stack is a subclass of java.util.Vector.

What is the flaw with the Stack class?

Stack class in Java violates the Liskov Substitution Principle and has some flaws. java.util.Stack is a subclass of java.util.Vector.

What is the flaw with the Stack class?


There is a flaw in the current stack class and it has become a matter of discussion online among people. Some feel it is a big mistake and should be changed immediately while for some it can be coped with.

Class java.util.Stack is a part of the java.util package and which extends java.util.Vector. It uses inheritance rather than composition and does not completely follow the Stack ADT (abstract data type).

Stack defines methods such as push(), pop(), and peek(). Being a subclass of Vector, it also inherits all the methods that Vector defines.

Stack being subclass of Vector violates the Liskov Substitution Principle.

Stack supports non-stack operations like inserting or deleting elements at any specified location using Vector's insertElementAt or removeElementAt methods.

Stack is a LIFO data structure one can use Vector's removeElement method to remove a specified element from a Stack object without regard to the element's position in the stack.

Normally List, Tree, Set, etc. are represented as interface in collection framework but Stack is a class.

The interface also exposes implementation details such as capacity.