Sunday, June 19, 2011

Overloading vs Overriding

Conceptually overloading and overriding is way different. Only the notion about interface name is same in both cases. Other than that, you cannot find a common thing between them.


Overloading is using the same interface but with different inputs and getting different behaviour as output. Slightly confusing right. I hope by end of this article you may feel better.


Not Overloading
Not Overloading


Overriding is in picture when there is inheritance. When you are inheriting a object there are so many behaviours associated with it. Primarily you are inheriting to use the common behaviour and attributes. But in certain cases, you may not like a particular behaviour. In such cases, you overwrite that behaviour alone in your inherited instance. The interface will be same.

Overloading is not polymorphism. Run time binding is not polymorphism. All these and more are used to exercise the property polymorphism. Polymorphism is a more general property belonging to object-oriented programming parlance and demands a separate article by itself. Let’s discuss that in detail then.

 

Overloading

Now let’s continue to discuss overloading and overriding. I have shown an image of a overloaded truck. I specifically used this image and drawn a wrong mark on it. Because, when I did some research on this topic using internet almost all web pages where ever overloading is discussed this kind of overloaded truck image is used to explain the meaning. Please get it right. Overloading is not adding more and more attributes and interfaces to the object so that it looks bulkier. In fact, when you use overloading for the outsiders view the object will look compact. That is putting more behaviour with same interface. That is your object will look sleek.


Before Overloading
Before Overloading


I have shown two images, one is Harmonium, a classical music instrument from India. Just for understanding it is a very trimmed version of piano. The other is a modern digital keyboard. Harmonium is without overloading and keyboard is after overloading. In a digital keyboard, keys are the interface, in programming method name. Speaker is the output, in programming return type of the method. When the player presses the keys he gives input to the method and gets output as music through the speakers.


After Overloading
After Overloading

Following line is very important to understand the complete correlation. When different input is given you get different out put. Here the interface (method name, keys) is same and output type is same. But the actual input (arguments, key press sequence) and output (returned data, music) is different. This is overloading. Look at the whole object (keyboard), how sleek it is after overloading.

 

Example java source code for overloading:

public class OverloadExample {
  public static void main(String args[]) {
    System.out.println(playMusic("C sharp","D sharp"));
    System.out.println(playMusic("C","D flat","E flat"));
  }
  public static String playMusic(String c, String d) {
    return c+d;
  }
  public static String playMusic(String c, String d, String e){
    return c+d+e;
  }
}

 

Overriding

It is very simple and easy to understand. When you inherit an object, you don’t like certain behaviour and so you replace it with your own. Note the word replace. Because after overriding the old behaviour is completely obsolete. Now look at the image of a monster truck. Here the old small four wheels are replaced with huge wheels to suit the current need. This is overriding.


Overriding


Some specific points about overloading in Java:

A java private method cannot be overridden because in first place it is not accessible to an inheriting object.
Final parameter in the overloaded method, has an interesting behaviour which I leave it for your exercise ;-)

 

An example for overriding from java package:


Implicitly every object in Java is extended from Object class. Object has a method named equals. This implementation compares the passed object with the current object and returns true if the reference are same. In String class you don’t want this behaviour. Therefore, equals method of the Object class is overridden in String class with its own implementation. Here the behaviour is modified to check if the character sequence is same as the compared one and returns true. This is a classic example of overriding.


No comments:

Post a Comment