Skip to the guide

Inheritance, and when it goes wrong

BeginnerConcept6 pages

Inheritance lets one class, the subclass, take another class's fields and methods and count as that class wherever it is expected. Interviewers want to hear what it gives you, the ways it breaks, and when you would choose something else.

A pizza franchise

Think of a pizza chain that sells franchises. Each new shop gets head office's manual: the recipes, how to take an order and how to ring up a group booking. The shop uses all of it on its first day. Customers walk into any shop with the chain's sign and expect the same pizza, because the shop is one of the chain.

The manual also says which pages a shop may change. There is one page for a local special, and the rest stays as head office wrote it. A shop that changed the house pizza anyway would break what every customer expects from the sign. A shop that built its own routine around how a procedure happened to work would break the day head office improved that procedure.