| Author |
Legal overridden method access
|
Jacob Michaels
Ranch Hand
Joined: Mar 23, 2003
Posts: 35
|
|
My question is: What does mean to override a method. Can someone show me an example of this. Thank you The rules for overriding can be summarized as follows: A private method may be overridden by a private, default, protected, or public method. A default method may be overridden by a default, protected, or public method. A protected method may be overridden by a protected or public method. A public method may only be overridden by a public method.
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
Method overriding deals with inheritence, where the parent and child class each have a function with the same name. Run this example to see what happens: Actually, the last line in main() illustrates polymorphism, which is another topic closely related to method overriding. Before you run it, you should predict what you think the output will be. Then see if it is what you predicted. Standard Disclaimer: the above code has not been compiled or tested. I created it from memory and should work as is. However, no warranty is given, explicit or implied. Use at your own risk. HTH Layne [ March 26, 2003: Message edited by: Layne Lund ]
|
Java API Documentation
The Java Tutorial
|
 |
Jacob Michaels
Ranch Hand
Joined: Mar 23, 2003
Posts: 35
|
|
Thank you That helped!
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
the parent and child class each have a function with the same name
This is also true of overloading, a totally different concept. So beware. To override: the methods must have the same namethe methods must have the same return typethe methods must have the same number of parametersthe methods' parameters must correspond one-to-one regarding the type of the parametersthe overriding method's access modifier must be of equal or weaker strictness (as original post)The overriding method can only throw a subset (or none) of the exceptions declared in the throws clause of the overridden method. (I've probably forgotten something else too) A method having the same name but different parameter list (number of parameters, or type correspondence) is overloaded. Examples are to be found in the Programmer Certification Forum. [ March 27, 2003: Message edited by: Barry Gaunt ] [ March 27, 2003: Message edited by: Barry Gaunt ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
Originally posted by Barry Gaunt: This is also true of overloading, a totally different concept. So beware.
Doh! Thank you for catching that. I meant to say "the parent and child class each have a function with the same signature," which is obviously very different. A methods signature is exactly what Barry described: the name and parameters. Of course, it doesn't seem this is completely accurate, either. I didn't know that you have to have the exact same return type to override a method as well. I guess this makes sense because the compiler will be thoroughly confused if the parameter list and name are the same, but the return type is different. I should code up an example program to explore some of the issues involved here.
|
 |
Junilu Lacar
Bartender
Joined: Feb 26, 2001
Posts: 4115
|
|
Originally posted by Jacob Michaels: A private method may be overridden by a private, default, protected, or public method.
Check out Marlene Miller's comments in this thread regarding private methods
|
Junilu - [How to Ask Questions] [How to Answer Questions] [MiH]
|
 |
 |
|
|
subject: Legal overridden method access
|
|
|