• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Overriding

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am trying to figure out why this would be legal as a override:
void method(int i, float f){}

but this would not:
protected void method(int i, float f){}

wouldn't they be the same except the first one is "default access"?
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tim Holmes:


I am trying to figure out why this would be legal as a override:
void method(int i, float f){}

but this would not:
protected void method(int i, float f){}

wouldn't they be the same except the first one is "default access"?




Give the code which you are trying to run.
As far as i know , you cannot override a method by making the access level more restrictive.
If you are overiding a public method then the over riding method should also be public .
Anything besides that would be restrictive like private, protected or package level acess.

How ever if your method is private , you can over ride it using coderanch.
As it can be less restrictive but not more.

Hope this helps you .
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How ever if your method is private , you can over ride it using coderanch.
As it can be less restrictive but not more.



To override a method, your class needs to inherit the method.Private methods are not inherited and thus cannot be overridden. You get a cmplier error if you try to override aprivate method.

Please correct me if i got it wrong.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry.My mistake!
 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim

I am trying to figure out why this would be legal as a override:
void method(int i, float f){}


That would not be a legal override cause you cannot make the access modifier of the overriden method more restrictive ( public to default in your case )...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic