Object-oriented design
Inheritance and polymorphism in practice
Overriding versus overloading, which method runs, and constructors in a hierarchy.
In two minutes
One idea to a line
Overriding replaces an inherited method with the same signature; which version runs is decided by the real type of the object, not the type of the variable.
Overloading means several methods of one name with different parameter lists; it is chosen at compile time from the declared argument types.
A parent constructor runs before the child constructor body. A child can call a specific parent constructor explicitly (super in Java, an initialiser list in C++).
In Java, fields and static methods are hidden, not overridden: they follow the declared type of the variable.
A subclass should be usable wherever its parent is expected without surprising the caller (the Liskov rule).
Slips people make here
Interviewers know them too
Expecting an overloaded method to be picked by the real type at run time.
Calling an overridable method from a constructor: the child's version may run before the child is ready.
Thinking a private method can be overridden.
Your practice here
Not started
12 written questions
No answers yet