Hi, What is overriding method and also use of that method? I also want to confirm that overloading method means more than one method we can define in same class.If i am right how can compiler know which method to take first!!! Please help me out. Thanks in advance! ADS
ADS, i think you are confusing overloading and overriding (a really easy thing to get confused over). Overloading is the process of defining multiple methods in the same class with the same name but different arguments in the function call. Overriding is defining the same method in a subclass (return type, method name and arguments) as is defined in a superclass (base class). why would i use method overloading? to allow for different types to perform the same method on the same class. why would i use overriding? to allow for the same method to perform different code depending upon which class the object is defined as. now that i have said that and confused you, let me give a couple of examples: example 1: i have a class line, a line can be defined by: two points or a point and slope. (this is an excellent time to use overloading) what we will do is define to defineLine methods: one that takes two point arguments, and one that takes one point argument and one slope arugment. the above is overloading - multiple methods with the same name in the same class. (note: the return types don't count in overloading) Example 2: we have two classes (overriding uses two classes) the class of Dog and the class of LittleDog. Both of the classes define the method talk( ). The Dog talk ( ) outputs "Bark Bark Bark" the LittleDog talk ( ) outputs "yipe yipe yipe". (see footnote 1). now when you define an object of type LittleDog and store it in a reference of type Dog and call the talk ( ) method; the output is "yipe yipe yipe". this is overloading. so the really easy answer to your question is: same class, same method name, possibly different return type, with different argument types = overloading. differnt class (one extends the other), same method, same return type and same arguments = overriding. note 1: before i get a lot of animal hate mail - i do like animals, i do volunteer on the board of directors at my county spca and no dogs were injured in the above examples.
Joined: Nov 22, 2008
Hi Steve, Great Explanation!!! Thanks a bunch! ADS
Originally posted by ADS: Hi, What is overriding method and also use of that method? I also want to confirm that overloading method means more than one method we can define in same class.If i am right how can compiler know which method to take first!!! Please help me out. Thanks in advance! ADS
Confusion seems to keep cropping up here because of jargon & different book wordings, etc.
OVERLOADING Method defined in same class with same name as long as parameters are unique. ?? In overloading methods, must the AccessModifier also stay the same, whereas overriding methods cannot be more restrictive ??
ADS, The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at "JavaRanch Naming Policy" . We require names to have at least two words (not just letters), separated by a space, and strongly recommend that you use your full real name. We want to get to know you as a Professional. Please choose a new name which meets the requirements and re-register.
Cindy Well, I just took a closer look at the DATE on the posts by ADS, and I guess this message is silly. Oh well. [This message has been edited by Cindy Glass (edited June 02, 2001).]
"JavaRanch, where the deer and the Certified play" - David O'Meara
Joined: Sep 29, 2000
Overloaded methods in the same class are treated as completely separate methods that just HAPPEN to have a method name that looks the same. Therefore they have no requirements relating to exceptions or accessibility. Overridden methods in subclasses are considered to be the "SAME" method with a different implementation. Suddenly that means that you have to think about if it will conflict with the superclass version of the method. Therefore you can not make the subclass method have a more restrictive access, and it must have the same return type, and it can not throw more checked exceptions (which would also make it more restrictive).
I think the one exception to more restrictive access modifiers in subclasses, is if no modifier is in the superclass. In that case, you can make it more restrictive (ie: private).
Joined: May 31, 2000
Thanks Cindy, That's a cool way of thinking about it.
Joined: Sep 29, 2000
Mike, I think that you are having a different conversation than we are. We are talking about accessiblity of METHODS when overriding. I am not sure what you are refering to. From the JLS 220.127.116.11 Requirements in Overriding and Hiding
If the overridden or hidden method has default (package) access, then the overriding or hiding method must not be private; otherwise, a compile-time error occurs.