Method overriding 00:00:00 Make a prediction You will predict which method runs for a base-typed reference and catch a signature change that silently defines a different operation. A Document variable points to an Invoice. You call its ordinary virtual preview operation. Do you see the base document preview or the invoice preview? The override on the actual invoice runs. The declared document type decides which call is available, and the receiver's runtime type selects the compatible override. 00:00:32 The good luck cards Every greeting card has a marked place where its writer signs off. A wedding card chooses a different closing line from a birthday card while keeping the familiar position and purpose of that line. The caller asks for the existing operation. An override replaces its implementation for a particular receiver. Adding another operation with an extra argument is like adding a second blank line in a different place. Method overriding means that a subclass provides its own implementation of a method that it inherits. Name and parameters stay the same. For objects of the subclass the new body replaces the old one, also when the call is made through a variable of the parent type. A caller can say chef.make dish without asking which chef it is. New kinds of chef arrive as new classes, and the caller stays as it is. That is the open/closed principle at work. 00:01:26 Select the receiver's implementation A variable typed as Document points to an Invoice. The caller invokes preview with the signature promised by Document. Invoice overrides that operation, so the implementation on the actual receiver runs. Changing the variable's declared type does not transform the invoice. Add a receipt subtype and repeat the same call. A test should demonstrate that both obey the same result contract even when their formatting differs. Make the base preview show the document title and let the invoice preview include its amount. Call preview through a variable declared as document. You should see the invoice amount. Replacing the receiver with a plain document changes the result without changing the call site. 00:02:09 Watch for accidental overloading A subclass adds a preview method taking an extra argument. A caller that invokes the original zero-argument signature may still get the inherited method. Use the language's override annotation where available. It catches a signature mismatch that otherwise looks convincing during review. Change the subclass to define only a preview that takes a width. A zero-argument call cannot use that new signature as an override. A compiler's override annotation can flag the mistake before a customer sees the base preview instead of the intended invoice rendering. In Java, static methods are hidden rather than overridden through runtime receiver dispatch. Check the kind of call before applying the virtual-method rule. An explicit override annotation catches a misspelling or wrong parameter list that would otherwise add a separate method. Java Object. equals, hashCode and toString are there to be overridden. The documentation states the contract: reflexive, symmetric, transitive, consistent, and equal objects have equal hash codes. Java LinkedHashMap. removeEldestEntry may be overridden to impose a policy for removing stale entries. A few lines make a small cache. 00:03:28 Try an interview follow-up How do you decide what may be overridden in a public library? Each open method is a promise for years. I close everything by default and open single hooks with a documented contract: when it is called, by which thread, what it may throw, how long it may take. I ship a test that a subclass author can run. A child task overrides execute but silently reports success when storage fails. The method signature matches. Is that sufficient? No. Callers also rely on the base operation’s observable failure contract. Force a storage error, run the public test once for the base and again for the child, and compare the result. A signature check cannot detect the swallowed failure. 00:04:16 What to take with you Overriding: same name, same parameters, a new body in the subclass. The variable decides what may be called, the object decides what runs. Keep the parent's promise: accept at least as much, return something that fits, throw nothing new, be at least as visible. Mark it with @Override or override, so that a typo becomes an error.