aspose file tools*
The moose likes Beginning Java and the fly likes Can overridden methods be less accessible?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can overridden methods be less accessible??" Watch "Can overridden methods be less accessible??" New topic
Author

Can overridden methods be less accessible??

Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
Posts: 60
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 ]

****************************<br />For Some Dreams.....<br />One Lifetime Is Not Enough.<br />****************************
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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.
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

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..


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
Posts: 60
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

Joined: Jul 11, 2001
Posts: 14112
With other words, being able to do so would be a violation of Liskov's Substitution Principle.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can overridden methods be less accessible??