• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Legal overridden method access

 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jacob Michaels
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
That helped!
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 name
  • the methods must have the same return type
  • the methods must have the same number of parameters
  • the methods' parameters must correspond one-to-one regarding the type of the parameters
  • the 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 ]
     
    Layne Lund
    Ranch Hand
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 7465
    50
    Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic