When you overload a method, that is within the body of a single class. (generally)
When you override, that is from class to class.
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).]
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?
posted 19 years ago
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
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
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.
posted 19 years ago
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