IS-A and HAS-A 00:00:00 Make a prediction You will test a proposed inheritance relationship in a real call site and use composition when the larger interface would expose invalid actions. A stack is backed by a list. If we let stack inherit arbitrary list insertion, what happens to the guarantee that the last pushed item is the next item popped? A caller can bypass push and insert or remove items at arbitrary positions. The implementation reuse has exposed a collection contract broader than the stack promises. 00:00:33 Guards wanted A guard can also be an employee: the ordinary payroll operation still works. The guard carries a radio, but sending the radio a payroll request makes no sense. Test the proposed relationship using behaviour. A subtype must substitute in the role; a collaborator that helps complete it belongs behind a has-a relationship. IS-A is a relation between types: every Dog is an Animal. In code: extends, implements, : public. It promises that a dog can be used wherever an animal is expected. The two look alike in a sentence, and they cost very different things. An IS-A is fixed when the class is written. The subclass takes everything from the parent, including what it does not want, and every change of the parent. 00:01:29 Why a stack has a list A caller pushes red then blue onto a stack. The next pop must return blue. If the stack inherits a full list interface, the caller can also insert between red and blue. This exposes operations the stack never promised. Hold a list privately and implement push and pop with it. The stack retains the storage while keeping one clear last-in, first-out contract. Push red and then blue. A public list method allows a caller to insert green at the bottom, or remove red directly. That result may be possible in the storage but violates the stack's restricted operations. Keep the list private and expose just push, peek and pop. 00:02:19 Check a tempting inheritance A manager is an employee if every employee operation still works for a manager. A department has employees: a department cannot stand in for one employee receiving one paycheck. Try the proposed subtype in a real call site before drawing the inheritance arrow. Send a manager through payroll: the usual employee identifier and pay rules still make sense. Try the same for a department containing five people. Which identifier and pay amount would it return? The ambiguity is evidence that the department groups employees instead of substituting for one. If a subclass narrows allowed inputs, taking the parent type at face value can break a legitimate caller. Two classes can hold similar data and still be unrelated types. Share the internal value or helper instead of forcing a public subtype relationship. java.util.Stack. It extends Vector, so a stack can be changed in the middle. The documentation recommends Deque and its implementations in preference to it. java.util.Properties. Because it inherits from Hashtable, put works on it. The documentation strongly discourages that, since it lets in values that are not strings. 00:03:40 Try an interview follow-up How would you prove that X is a Y? List Y's promises, also the ones that are not in the signature. Write a test per promise and run the suite on X. A failing test means: weaken Y's contract if callers can live with it, or use HAS-A. A coach says a team is a group of players. Does that make a team a kind of player in your program? Try passing the team into a substitution method that expects one player and one jersey number. The promise breaks. Model the roster as a relationship and let the team own roster rules. 00:04:20 What to take with you Say the sentence, then test it. IS-A needs four yeses: a true kind, for its whole life, with every inherited promise kept, and someone who uses it through the parent type. Otherwise HAS-A: a field with a small contract. Parts can be swapped, shared, faked and combined.