Static methods are hidden
Overriding means a subclass gives its own version of an inherited instance method with the same name and parameters. @Override asks the compiler to check that you really are overriding, which catches typos and wrong signatures.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Create Shape with String name() returning "shape" and Square overriding it with @Override. Then add a method name(String prefix) in Square. Print which ones are overrides and which is an overload by calling them.
Write Document with Document copy(), and Invoice extends Document with a covariant Invoice copy(). Show that invoice.copy() needs no cast.
Write a correct equals(Object) and hashCode() for Point(int x, int y) and show list.contains and set.size() behave correctly.
Explain it without notes
List the rules an overriding method must follow.
What's the difference between overloading and overriding, in terms of when the decision is made?
Why does public boolean equals(Point p) break HashSet and ArrayList.contains?
What is a bridge method and why does javac create one for a covariant return?
Expected output
Parent.who (static)
Child.me (instance)
Child.who (static)