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 />****************************
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.
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..
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
subject: Can overridden methods be less accessible??