This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Weaker Access Privilege Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Weaker Access Privilege" Watch "Weaker Access Privilege" New topic
Author

Weaker Access Privilege

Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
hi,

when we override why cant we grant weaker access to overridden methods...i need ans in object oriented way....if weaker access to granted in subclass what are all possible problems that could come up..ans with example would be more elaborative



Thanks
user101
Problems And Solutions - Algorithms
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Liskov substitution principle


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

In what sense do you mean "weaker"? I mean, you can change a protected superclass method to public. Overriding a private superclass method obviously doesn't make sense.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

I assumed Satyajit meant more restrictive - e.g. changing public into protected.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

To me, that'd be "stronger"--that's why I had to ask :)
Satyajit Bhadange
Ranch Hand

Joined: May 13, 2010
Posts: 104
ya i meant changing public to protected..why it cant be done...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The link provided gives a pretty detailed explanation.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16


Because there is an is a relationship between the superclass and the subclass (the Liskov substitution principle that Rob mentioned). If this were allowed, there would be a backdoor through which you could call methods that should not be accessible.

You can assign an instance of a subclass to a variable that has the type of the superclass:

The call in line 17 would be possible, because method() is public in class Super. But it should not be allowed, because obj is really referring to an instance of Sub, and in that class, the method is protected! So, there is a backdoor through which you could now suddenly call a method in class Sub that should not be accessible from the outside.

To prevent this, it's not allowed to give an overriding method more restricted access than the method it is overriding.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
jQuery in Action, 2nd edition
 
subject: Weaker Access Privilege
 
Similar Threads
Abstract methods and access modifier
Question from go4java mock
Help on SimpleTag
method visibility - Overriding methods can NOT be more restrictive
which are the correct answer