Multiple parents in single inheritance 00:00:00 Make a prediction You will compose independent interfaces on one object without importing two conflicting implementations or giving a client the wrong authority. A file supports both read and close. Must a parser receiving the file also be able to close a stream its caller still owns? No. The same object may fulfill both contracts, but the parser should receive only the reading capability if cleanup belongs elsewhere. Multiple roles need not imply multiple owners. 00:00:33 Pip and the jobs on the beach At the beach one helper can carry parcels and also check tickets. A visitor asking for a ticket check does not acquire authority to direct all the helper's parcel deliveries. Multiple interfaces identify roles; a client can be handed just the role it needs. Combining two implementations with state is a separate, more demanding choice. In a single inheritance language a class may extend one class only. A duck is a bird, so the one place is taken. It must also be usable where something that swims is asked for, and where something that flies is asked for. Without these tools people copy code or build a class for each combination. Two abilities need three extra classes, and four abilities need up to fifteen. The tree grows although no new idea was added. 00:01:24 Give a file two capabilities An input file can satisfy two independent promises. One client reads bytes; another owns cleanup and calls close. Implementing both interfaces lets the same file participate in both roles without inheriting storage from two parent classes. Give a reader only the reading contract when it should not close a resource owned elsewhere. Test ownership by checking who releases the handle. Read one line through the reader interface, then close through the closeable interface. A later read must see the same underlying closed handle. Giving each capability its own independent file would make close appear to succeed while the reader keeps operating. 00:02:10 Resolve competing defaults deliberately Two interfaces both supply a default label. A class implementing both may have to override the method and choose a meaning explicitly, depending on the language. If the two labels mean different things, use separate operations instead of silently picking one. Use the object first on an envelope and then in a compact display. A short display name may omit the destination address; it cannot silently satisfy both uses. Name the two operations explicitly or choose one documented shared meaning and test each intended caller. Two default implementations with the same signature can conflict even when both interfaces are otherwise useful. A language's lookup rule cannot decide which meaning the user intended. Split the roles or implement the shared operation explicitly when its meaning really is common. A Java array list extends one implementation class and implements multiple interfaces. One interface supplies list operations while another can signal efficient indexed access. Default methods also let a library add reusable behaviour to an interface. They still require explicit conflict resolution when two unrelated defaults promise different implementations. 00:03:30 Try an interview follow-up Why prefer composition when you need behaviour from two sources? A part can be chosen per object, exchanged at run time and tested alone. It can have state. The price is forwarding lines and one more object. A class implements two parent interfaces that both offer reset. One means rewind to the beginning, the other means discard the current job. Show a call for which both meanings cannot be correct. Use explicit interface methods where supported, or present separate adapters so each caller receives its own promised operation. 00:04:12 What to take with you Split the wish: type, behaviour or state. Interfaces, default methods and parts answer them in this order. Extend one class only where the child truly is the parent for life. Promise abilities with small interfaces. Put what differs or changes into parts.