Overriding is when a method with the same method descriptor as a method in the parent class is implemented in the subclass. Overloading is when methods with the same method name, but different method signature is implemented in the same class.
Method Signature and Descriptor
The method signature is the method name and the number of parameters, type of each parameter and the order of its parameters.
C:foo(B, B)
The method descriptor is the method signature and the return type.
A C:foo(B, B)
Overriding
When a method in a parent class is reimplemented with the same method signature in the subclass, it is called method overriding. This is useful for altering and extending behaviours of existing classes.
The toString method exists in the Object class which is a supertype of all classes.
Implementing a toString in a subclass like Circle allows us to control the string representation of the Circle objects.
When overriding a method, the @Override annotation should be used.
This helps the compiler know that the method is intended to override a method in a parent class, and notifies us if there is a typo or overriding is not possible.