aspose file tools
The moose likes Beginning Java and the fly likes Overloading & Overriding! Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Overloading & Overriding!" Watch "Overloading & Overriding!" New topic
Author

Overloading & Overriding!

Angela Jessi
Ranch Hand

Joined: Nov 27, 2000
Posts: 428
Hi,
(1) What is Overloading?
(2) what is Overriding?
(3) What is difference between Overloading and Overriding method?
Thanks in advance,
angela
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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).]
Brad Ford
Ranch Hand

Joined: Mar 22, 2001
Posts: 40
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

Joined: Nov 27, 2000
Posts: 428
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

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Nov 27, 2000
Posts: 428

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
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Just to clarify:
When you are overloading, the parameter signatures must be different.
When you are overriding, the parameter signatures must be the same.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Nov 27, 2000
Posts: 428

Thanks a bunch Thomas & Mike
I really appreciate ur responses.
Have a nice weekend
Angela
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Overloading & Overriding!
 
Similar Threads
What are the new things of Overloading and Overriding in JAVA5?
Overriding
overriding , overloading etc ...
Polymorphism,Overloading, Overriding
overriding and overloading