A stack only allows push, pop and peek at its top. Build it twice, on top of ArrayList<String>:
InheritedStack extends ArrayList<String>— addspush,pop,peek(inheritance, "is-a");ComposedStack— has a privateArrayList<String>field and offerspush,pop,peek,isEmpty,size,toString(composition, "has-a").
TwoStacks reads words until the end of the input and pushes each word on both stacks. Then it shows the weakness of the first design: inherited.add(0, "INTRUDER") and inherited.remove(inherited.size() - 2) compile and break the stack discipline, while the same calls on the composed stack do not even compile (leave that line as a comment). Finally both stacks are emptied. For the input
red green blue
the program prints
Inherited [red, green, blue], top blue
Composed stack: [red, green, blue], top blue
After add(0, ...) and remove(...): [INTRUDER, red, blue]
Popping the inherited stack: blue red INTRUDER
Popping the composed stack: blue green red