• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Overriding

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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
  • 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 public.
As it can be less restrictive but not more.

Hope this helps you .
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How ever if your method is private , you can over ride it using public.
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
  • Quote
  • Report post to moderator
Sorry.My mistake!
 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • 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 )...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!