Inheritance vs composition 00:00:00 Make a prediction You will decide whether two classes share a behavioural contract or only a reusable piece of work. A detailed report can go to email or file, and a brief report can do the same. If we add print delivery, how many existing report formats need a new delivery subclass? With one subclass per combination, both formats grow another subclass. If the report holds a delivery channel, one print implementation serves either format. The question is what varies independently. 00:00:32 The boat stencil A boat stencil gives every copy its hull shape. Fitting a sail or a motor afterward lets the same hull travel differently. Cutting one new stencil for every sail-and-engine combination duplicates the hull drawing. Use inheritance when a subtype genuinely keeps its parent's promises. Use composition when a part supplies an independently changeable capability. Inheritance: a new class (subclass) is made from an existing one (superclass). It gets all its fields and methods, may add some and may override some. It also becomes a subtype: it can be used wherever the parent is expected. This is the is-a relation. The choice decides how hard the code is to change. A subclass depends on how its parent works inside. An object that holds a part depends only on the part's public methods. 00:01:26 Change how a report is delivered A report can be brief or detailed and delivered by email or file. Subclassing every combination gives four classes before any other option exists. Keep the report's content decision on the report and pass it a delivery channel. Sending delegates to that channel. A detailed report can now use either channel. Adding a third channel leaves the report variants untouched. Generate the same brief report twice. Swap the email sender for a file writer on the second run and compare the report content. It should stay identical. If switching channel requires rebuilding the report's domain state, the two independent decisions have become unnecessarily coupled. 00:02:08 When a subtype really does fit A read-only report can be a subtype of a readable document when every promise is about reading. It fails as a subtype of an editable document if save always throws. Check what callers are allowed to do; a shared field or shared implementation alone does not establish substitutability. Pass the read-only document to a previewer and then to a caller promising to save changes. Preview works. Save either throws or silently discards the edit. Separate readable and editable capabilities so the unsupported promise can be caught before the caller accepts the object. A child class that throws on every write cannot replace an editable parent whose contract promises writing. Sharing fields is insufficient. A subtype can be appropriate when all parent behaviours remain valid, such as a read-only specialization of a purely readable interface. Java Stack. It extends Vector, so a stack also has insert by index. The documentation says the Deque interface should be used in preference. Java Properties. It inherits from Hashtable. The documentation strongly discourages put and putAll, because they let callers insert entries that are not strings. An inherited method too many. 00:03:29 Try an interview follow-up How would you design a base class that other teams extend? I close most behaviour by default and open only the hooks other teams actually need. For each hook, I document its inputs, timing, threading assumptions and possible failures. I avoid invoking overrides from a partially initialized constructor and provide a shared test suite. I also leave a composition option for clients that want the contract without the base implementation. A delivery bike can gain or lose an electric motor. If its class encodes both vehicle and power source, replacing a motor may require replacing the bike object. Keep the bike's identity and give it a replaceable propulsion component. Trace the upgrade and check that its rental history survives. 00:04:18 What to take with you Inheritance reuses code and promises a type in one step. It fits a true is-a that can stand in for its parent, in a small hierarchy that was made for it. Outside of that it brings unwanted methods, fragile parents and too many classes. Composition holds parts behind small interfaces and passes work on. It costs some forwarding code and buys loose coupling, free combination and replacement at run time. Start with composition, and inherit when you can say why.