Free Object-oriented basics lessonsObject-oriented basics · Stage 2 · Inheritance and polymorphism · Step 1
Inheritance
How a subclass reuses and extends a class, in what order objects are built, and when inheritance is the wrong tool. About 13 minutes.
A class Stack extends ArrayList so it can reuse add and remove. What is the main problem?
- ACallers can use every ArrayList method, such as inserting in the middle, which breaks the stack contract
- BIt is slower
- CIt won't compile
Show the answer
Callers can use every ArrayList method, such as inserting in the middle, which breaks the stack contract. A Stack is not an ArrayList in the way its callers rely on: inheriting exposes everything.
4 questions on this idea
Short questions that apply the idea to new cases: trace it, work out a number, put steps in order. A wrong answer explains itself.
Answer them in your browserFree account, no card. Your progress is saved, and the idea comes back in a quick review later.
Sign up free to solve