aspose file tools*
The moose likes Beginning Java and the fly likes overriding Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overriding Method" Watch "overriding Method" New topic
Author

overriding Method

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 557
    
    7

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.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
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 ??
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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).
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hi Cindy,

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).
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
Thanks Cindy,
That's a cool way of thinking about it.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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 8.4.6.3 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding Method
 
Similar Threads
overriding
Overriding method doubt
how to put if condition in validate.xml
More Java 5 Questions
thread