Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

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 )...
 
Are we home yet? Wait, did we forget the tiny ad?
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic