Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Overloading & Overriding!

 
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
(1) What is Overloading?
(2) what is Overriding?
(3) What is difference between Overloading and Overriding method?
Thanks in advance,
angela
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you overload a method, that is within the body of a single class. (generally)

When you override, that is from class to class.

Examples:

MethodA in class A is overloaded... the parameter list is different. So when you call it with MethodA(1, "Hello") or MethodA(1,2), the run-time determines which MethodA to use by it's signature (the thing between the ()'s ). This is why for a single overloaded method, you must ensure all method signatures are unique.

This is an example of an overriden method.
Class B completely replaces MethodB (the inherited MethodB is hidden).
Class C also hides MethodB, but because it calls super(), we say that Class C's MethodB extends Class A's MethodB.
The above code results in:
"MethodB in Class A" <- from a.MethodB()
"MethodB in Class B" <- from b.MethodB()
"MethodB in Class A" <- last two lines are from c.MethodB()
"MethodB in Class C"

[This message has been edited by Mike Curwen (edited March 30, 2001).]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simple explanation sans examples:
Overloading a method is defining multiple methods with the same name but different parameter signatures. The method executed depends on the parameters passed.
Overriding a method is naming a method in a subclass the same as a method in the superclass, thus replacing that one.
Is this an oversimplification?
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all ,
Can I pass different signature parameter in Overriding method? What is advantage of Overloading method and overridding method?
Thanks in advance,
Angela
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Can I pass different signature parameter in Overriding method?


If you're asking what I think you are, then yes.

If you define MethodB in Class A, and two MethodB's in Class B, then the MethodB in Class B whose signature matches the MethodB in Class A -- then that MethodB would be overriding. The *other* MethodB would be overloading MethodB in Class B. phew!

What is advantage of Overloading method and overriding method


Polymorphism.

Every object in Java has a method called toString(). Why? Because toString is declared in Object, which all classes in Java descend off of. Any and all classes that you can include or make in your programs already have a definition for toString(), which is supplied way back in Object (or maybe not, but just think that way for a second). But what *you* as a programmer can do is override toString(), so that it exhibits specialized and unique behaviour for your class. So instead of the Object information you get when you don't override toString(), you can redefine the method to output more exact and/or useful information (a JComboBox might have it's toString() overriden to output the text of the currently selected item.)

Because this is the same method (toString() ) but different behaviour, we say it is polymorphic.

Overloading is a shortcut for programmers. It lets us be a little bit lazy. Say for example that StringBuffer class didn't overload it's append() method. StringBuffer currently has an append() method for every one of the primitive types, and one for String and one for Object. If Sun didn't overload append(), we'd have to code something like:
This makes life easier.
Unfortunately you can't specify a different return type and overload a method. Otherwise, JDBC would be a lot simpler.
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Mike
So let me clear here:
(1)Overloading method means you can pass different parameters signature to different class....
Also Overloading method method signature i mean return type & method name must be same.
(2) Overriding method means you have to pass same parameters signatures to different classes.
Please let me know if i am wrong :rollereyes:!!!
Thanks again,
Angela
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify:
When you are overloading, the parameter signatures must be different.
When you are overriding, the parameter signatures must be the same.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Thomas! I probably wouldn't have been as succinct

But for I can't help but complete the answer :

Must be the same
Must be different

Overloading:
return-type Method-Name(signature)

Overriding:
return-type Method-Name(signature)

[This message has been edited by Mike Curwen (edited March 30, 2001).]
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks a bunch Thomas & Mike
I really appreciate ur responses.
Have a nice weekend
Angela
 
    Bookmark Topic Watch Topic
  • New Topic