Overloading vs Overriding

03/10/2009

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.

Nice comprehensive information sir….Thanks.

Suresh on October 7th, 2009 11:56 pm

Nice example to understand the concepts easily Thanks

chaitanya on December 18th, 2009 10:55 pm

Nicest example that I have ever seen or think of for these blury concepts “overloading” and “overriding” in my 12 years exposure to java. Your simple way of explaining is superb! Hearty thanks & wishes!!

Andy on January 15th, 2010 2:42 pm

very very thank u sir …very nice explanation sir

veera on January 27th, 2010 7:23 pm

thanks a lot for explanation………..

dipankar on February 4th, 2010 5:24 pm

This information is superb.Thanx alot.I understand this topic very easily by those examples. Thanq

sujana on February 23rd, 2010 6:32 am

A nice way to explain OL and OR.

Vishal on June 15th, 2010 11:45 am

Awesome…

Muthukumar on August 1st, 2010 2:15 pm

Nice Observation. Thanks

Mitesh Agrawal on November 29th, 2010 11:11 am

Great explanation

Asim Nawaz on December 2nd, 2010 3:55 pm

wow…simply superb

ps.krishna on December 28th, 2010 6:14 pm

thnx sir it is helpful for understanding overloading and overriding.
thanks

mukesh on March 4th, 2011 3:00 pm

Nicest example to explain the two confusing concepts of OOPS.

thank u sir

amit on March 15th, 2011 6:02 am

sir,overriding is explained very nicely but overloading concept is also nice but the use of word”interface” is quite confusing….

Anonymous on April 12th, 2011 12:26 pm

really good!!! thks

Anonymous on April 14th, 2011 1:37 am

(it is Just Good For Beginers)..
And…. It is not Sufficient…for others
Give Two three Programatical Examples…

RaamNaresh Reddy on April 29th, 2011 10:17 am

Nice explanation!

Sonoo jaiswal on May 21st, 2011 6:47 am

Can you give a code example for over riding like over loading….?

Zeeshi on June 13th, 2011 5:22 pm

Overloading simplest example

public class OverLoad
{
void sum(int x, int y)
{
int sum=x+y;
System.out.println(“sum is: “+sum);
}
void sum(int x, int y, int z)
{
int sum=x+y+z;
System.out.println(“sum is: “+sum);
}
public static void main(String arg[])
{
OverLoad ol=new OverLoad();
ol.sum(5,4);
ol.sum(5,4,3);
}
}

o/p-> sum is: 9
sum is: 12

Overriding simplest example

class A
{
void calculate(int x, int y)
{
int sum=x+y;
System.out.println(“sum is: “+sum);
}
}
public class B extends A
{
void calculate(int x, int y)
{
int product=x*y;
System.out.println(“product is: “+product);
}
public static void main(String arg[])
{
B b=new B();
b.calculate(5,4);
}
}

Run this code o/p will be 20

class A
{
void calculate(int x, int y)
{
int sum=x+y;
System.out.println(“sum is: “+sum);
}
}
public class B extends A
{
/*void calculate(int x, int y)
{
int product=x*y;
System.out.println(“product is: “+product);
}*/
public static void main(String arg[])
{
B b=new B();
b.calculate(5,4);
}
}

run this code o/p will be 9

now i think u are clear… if still not. leave the comment

Nitin on June 22nd, 2011 8:53 am

Simply great!!!!

Mukesh on June 22nd, 2011 9:39 am

Great!!!.nice real world eg. to explain the core concept.

Eshu on June 26th, 2011 5:54 am

Thanx buddies….!!!

Nitin on June 28th, 2011 3:38 am

your concept is very clear please send me some concept and example in my email related to applet,exception ,thread… Thanks

Mukesh kumar on July 2nd, 2011 10:30 am

Joseph , I could not digest “Run time binding is not polymorphism”.But I believe it is otherwise. Overloading is static binding since at compile time it is bind,And also overloading is not polymorphism”, whereas overriding is polymorphism since it is binded at runtime.

Clear the confusion

Abhiram on July 4th, 2011 11:51 am

overloadin=it is the relation biw the method declare in supper class and subclass having thr same method name name diffirent no input and diffirent type of input–;;; for eg
public void m1()// no argument constructer
public void m1(int i)// argumenter contructer
public void m1(inti,String s)// this is all about the method overloading

anand on July 6th, 2011 11:08 pm

Nice Explanation

Raja Ramesh on July 14th, 2011 12:38 pm

Dear Joseph, this is the very simple and easy to understand article.so we are thank to you………………

Kiran on July 14th, 2011 4:05 pm

If example is given for overriding then it would be possible to understand more clearly….

Bharati on July 18th, 2011 11:37 am

example is given above in my comments

Nitin on July 21st, 2011 8:59 am

Is it true that Overloading is not a polymorphism?

Praveen on July 21st, 2011 11:34 am

this overloading and overriding concept is very clearly explained,and before reading this topic i didn’t understand these two task difference,now am understand completely. thank to you

chandru on July 26th, 2011 8:22 am

nice sir…

usharani pasala on August 2nd, 2011 9:49 am

Hi joe!
So gud!

Ram Gowtham on August 8th, 2011 7:43 am

All your answers are awesome relating to realtime easy understanding thank you for sharing your knowlegde. can you please explain java polymorphism it hasnt been very clear to me all time although partially i understand still not clear.

thanks
raj

raji on August 19th, 2011 8:14 pm

safgs

dg on October 14th, 2011 1:36 pm

Hi,

In one of the interview I got this question:

Consider this example:

class P {

public void abc() {

}

Now class C inherits class P

class C extends class P{

public void abc() {

// adds some additional functionality;

}
Now, here is the question:
Due to some reasons if you want the functionality of abc method of class P (i.e parents )in child’s class, then how we can achieve this?. Is it through key word super?

I understand this is very strange, but it was asked in interview

Thank u,

Anonymous on October 21st, 2011 4:02 pm

Give an example when you can not overload a method in Java.

keerthi on October 27th, 2011 2:03 am

nice :))

dennise on November 16th, 2011 10:38 am

wow simply super…..great explanation .thank u

sree on November 18th, 2011 2:36 pm

hi its really useful 4 me to understand tanqu…

Archu on November 19th, 2011 11:52 am

Consider this example:

class P {

public void abc() {

}

Now class C inherits class P

class C extends class P{

public void abc() {

// adds some additional functionality;

}
Now, here is the question:
Due to some reasons if you want the functionality of abc method of class P (i.e parents )in child’s class, then how we can achieve this?. Is it through key word super?

I did not understand the question here..
Can’t we do like this –

P sup = new P();
P sub = new C();
sup.abc(); //Calls super class method
sub.abc(); //Calls sub class method

Please let me know if its not correct..

Thanks,
Deepesh

Deepesh on November 21st, 2011 5:50 am

Thanks Sir, its very nice and very easy to understand that.

kashif on November 29th, 2011 6:35 pm

thnx sir it is helpful for understanding all concepts. Iam a beginner,I want to download all your core concepts for java, How Can i download, please send to my mail.

Thanxs a lot, great job!!!

vasu pulavarthi on November 30th, 2011 6:37 am

can u override the static method?many of people asking this question.
YES OR NO
IF YES GIVE AN SUITABLE EXAMPLE WITH EXPLANATION.

edukondalu a on December 9th, 2011 4:25 pm

PLEASE HELP ME IN ABOVE QUESTION…

edukondalu a on December 9th, 2011 4:26 pm

AOA
Very Informative Post & Blog – Keep doing Sir :)

Best Regards;

Muhammad Kamran on December 12th, 2011 7:56 am

i want more clarification plz give more without confusion

Anonymous on January 19th, 2012 12:47 pm

i want more clarification

sravan on January 19th, 2012 12:48 pm

can the main function be overloaded??
(public void static main(String []args))

malay ranjan sahu on January 20th, 2012 11:49 am

this is the very simple and easy to understand article.so we are thank to you………………

Mayur on January 24th, 2012 12:08 pm

good one :-)

Anonymous on January 25th, 2012 2:34 pm

[...] of ‘a class’. Unlike inheritance, you can choose any single object of a class and modify its behaviour leaving the other instances [...]

Decorator Design Pattern&hellip on January 29th, 2012 6:34 pm

Thanks for explaining in a simple language.

K S Ravikumar on February 2nd, 2012 10:30 am

Good Work :)

Gokul on February 2nd, 2012 11:22 am

hi jo..thank you so much…its very useful website…

Karunakar

Kannaaa on February 7th, 2012 11:20 am

There are times when your article is complete and one requries no more reading.

this is NOT one of them. Please try to put some examples and rules/protocol associated.
eg: you didnt explain the access modifiers / scope in over-riding
eg: return type in overloading

gud effort nevertheless…

Java Learner on February 9th, 2012 11:20 am

Thankyou so much..!

Anonymous on February 9th, 2012 4:01 pm

Nice one sir….

Anonymous on February 11th, 2012 4:23 pm

excellent !
can u give some more example in c++

Shilpi

shilpishilpisrivastav on February 13th, 2012 6:05 pm

Amazing way of drilling the concept of Overloading and Overriding into the head.

Thanks Joe

Noel on February 14th, 2012 11:34 am

can return type differ in overloading and overriding?

pranav on February 18th, 2012 12:04 pm

Nice one, Thanks…..

mubashir on February 26th, 2012 5:12 pm

nice explanation but little bit confusion can u please explain it clearly…….

seshu dheeraj on February 28th, 2012 5:54 pm

Really good and practical

Ajit on February 29th, 2012 2:03 pm

nice artical

Anonymous on March 2nd, 2012 7:39 pm

@Nitin:Good one…:-)

Sam on March 7th, 2012 12:04 pm

very nice example with real world…:)

Anonymous on March 7th, 2012 3:09 pm

Thank you sir :) i got to know the concept

Anonymous on March 14th, 2012 12:23 pm

can you override the static.

pallavi on March 21st, 2012 11:36 am

no you cannot override the static

sabarishreddy on March 22nd, 2012 3:59 pm

i really thanks to you very much ……i understood both concept very well.

puneet verma on March 24th, 2012 12:46 am

nice

prakash on April 9th, 2012 10:59 am

Awesome explaination…thanks

Vijay on April 16th, 2012 6:16 pm

thnks sir nyc explaination………..

abhi shukla on April 20th, 2012 12:27 am

thank u sir

loving example and awlanationesome exp

Anonymous on April 24th, 2012 3:48 am

Nitin given this example to explain overriding but i have a doubt in this below example that is “What is the use of extending A class in B?” without extending that class also we will get the same result na…

Overriding simplest example

class A
{
void calculate(int x, int y)
{
int sum=x+y;
System.out.println(“sum is: “+sum);
}
}
public class B extends A
{
void calculate(int x, int y)
{
int product=x*y;
System.out.println(“product is: “+product);
}
public static void main(String arg[])
{
B b=new B();
b.calculate(5,4);
}
}

Run this code o/p will be 20

Raju on April 26th, 2012 10:53 pm

Good explanation with nice examle
thank uuuuuu sir

sathya on May 11th, 2012 11:19 pm


Email:

about
I am Joe, author of this blog. I run this with loads of passion. If you are into java, you may find lot of interesting things around ...more about me. Google+
java badge
Home