File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between overloading and overridding? (was: SCJP) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Difference between overloading and overridding? (was: SCJP)" Watch "Difference between overloading and overridding? (was: SCJP)" New topic

Difference between overloading and overridding? (was: SCJP)

saisreereddy surabhi

Joined: Feb 18, 2007
Posts: 7
i want to know the difference between overloading and overridding.........please let me know this
[ February 19, 2007: Message edited by: Henry Wong ]
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
The following link will explain this for you.

Method overloading and overriding

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
I've an extra point to make.

In JAVA 1.5. you're allowed to change the return type in the overriding method as long as the new return type is a subtype of the overridden (superclass) method. This is called covariant returns.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Hello "wilhelm tell"-

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Hi Surabhi

Overloading - is a feature - same method name, different arguments-list

Overriding - defining additional specific behavior for Subclass

the above defines work along with some facts with access modifiers and variations, you could remember those from book.
Atul Sawant
Ranch Hand

Joined: Jul 06, 2006
Posts: 304
Overloading - similar/Analogous.
Overriding - same.

Suppose there is a class called Animal which has a method called 'reproduce'. then you have two subclasses mammals and non-mammals both of these have a method called 'reproduce' too (too obvious). So, do they need to override the method of Animal class or overload it?

It would be overriding because if you call the method 'reproduce' from Animal object then it will show you reproduction details which might be mammal or non-mammal however if you call a method 'reproduce' from mammal/non-mammal object it will be specific and it will/has to show reproduction details of mammal/non-mammal only! this is done by overriding the Animal's method by giving same argument list and return type.

The decision is taken on runtime for which method to be referred. This is also called as dynamic binding.

Suppose there is a class called Jeff. He has several roles in his life (lets take two roles into consideration, Father and a Manager) so lets say there is a method called 'Role'. According to the argument list provided the different roles would be considered i.e. either father or Manager.


void Role(Father obj){......**roles of father**.....}
void Role(Manager obj){......**roles of Manager**.....}

if you call the method saying objectofJeff.Role(objectofFather) then the method 'void Role(Father obj){......**roles of father**.....}' would be considered...

The decision is taken at compile time.

I hope this was clear.

There are several conditions to check so that a method is properly overriddenn/overrided or overloaded. You need to know them by heart.

Mission SCWCD. Mission SCJP Complete: SCJP 1.4 - 91%
I agree. Here's the link:
subject: Difference between overloading and overridding? (was: SCJP)
It's not a secret anymore!