Overriding and overloading

The terms overloaded and overridden are similar enough to give cause for confusion. My way of remembering it is to imagine that something that is overridden has literally been ridden over by a heavy vehicle and no longer exists in its own right. Something that is overloaded is still moving but is loaded down with lots of functionality that is causing it plenty of effort. This is just a little mind trick to distinguish the two, it doesn't have any bearing of the reality on the operations in Java.

Overriding is a term related to inheritance. A method is overriden in a subclass when a substitute method of exactly the same name, parameters and return types is implemented and thus replaces the version in the parent class. With an overriden method its entire functionality has been replaced in the child class.

Overriding is useful in that it allows a programmer to inherit most of the functionality of one class but to change that functionality slightly by replacing one or more of the methods in the child class. The new (overriden) method will appear to be exactly the same to any other programmer as it will have the same signature, i.e. Name, parameter types and return type, but what it actually does, its functionality will be different. This important in that it allows the programmer to keep the same interface, the outward appearance whilst changing the inner workings.

The core Java language includes an example of overriding starting with the great grandparent Object class. Every object in Java is implicitly a child (or ancestor at least) of the Object class. The Object class has a method called equals, which in the default implementation will compare the memory address of two different classes. This is rarely what is wanted. So if you had two String objects you would probably want the equals method to compare the sequence of characters within each object. So it is no surprise that the Java String object does indeed override the equals method with a new version that makes a character by character comparison.

Last modified: Thursday, 24 July 2014, 2:54 PM