• 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Can overridden methods be less accessible??

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,Ranchers, I have been trying to override a method and give it less accessibility, but I came to conclude that methods,cannot be less accessible,if they are overridden in a derived class, my question is why?and can I somehow achieve that? and does this also apply to methods overloading? thank you
this the code I tried:

This program showed the following error:
F:\MyJava\Swing&Others\UseBothOf.hava.java:10: doSomething() in child cannot override doSomething() in Parent22; attempting to assign weaker access privileges; was public
private void doSomething()
^
1 error

Tool completed with exit code 1
[ January 22, 2008: Message edited by: Ammar Salem ]
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ammar Salem:
Hi,Ranchers, I have been trying to override a method and give it less accessibility, but I came to conclude that methods,cannot be less accessible,if they are overridden in a derived class, my question is why?

Would you believe because that's how it's defined? But more to the point, I would like to be able to use a child anywhere I could have used the parent. If you change the child's overridden method signatures, you will break this contract.

and can I somehow achieve that?


Why would you want to?

and does this also apply to methods overloading?

I'm not sure I get you here.
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overriding is like a father has given his child something to use..now child can use that thing(method) as it is..or he/she can modify that before using..!!! This is a rule in Java..a father must give all his properties(that has has received from his father) to his child..now after child receiving the method, he/she has to pass that method to sub child..!! Because grandchildren
has the rights to know their grandfather,grand grand father(so on..)properties..!!

The only way you can restrict the passage is to Overload the method..!!!
Yes, you can restrict the access level with Overloading..
 
Ammar Salem
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both, pete stein and Sunny Jain.You both made clear enough to me.I appreciate your kind help.
Ammar Salem
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With other words, being able to do so would be a violation of Liskov's Substitution Principle.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!