File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can overridden methods be less accessible??

 
Ammar Salem
Ranch Hand
Posts: 60
  • 0
  • 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 ]
 
pete stein
Bartender
Posts: 1561
  • 0
  • 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.
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • 0
  • 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
  • 0
  • 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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic