aspose file tools*
The moose likes Beginning Java and the fly likes Access modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access modifiers" Watch "Access modifiers" New topic
Author

Access modifiers

Shruthi Sam
Ranch Hand

Joined: Jul 09, 2008
Posts: 73
Hi,
Suppose i am having a class x which contains a protected method. The class x resides in a package say A. Now another class in package B extends this class x in package A and tries to override the protected method in class x. Now what will be the access modifier of this method in the subclass? Is it private? will the compiler fail if we assign "weaker" privileges to the overriden methods? Please clarify.


Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

try to implement . what error you are getting in compile-time ? Google for private,package-private(default),protected,public access specifiers
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
while overriding the method the scope/access of the method cannot be more restricted.

For eg, in your case the protected method can be overriden with public and protected modfier. if you put private, compile time error will be there

similarily public method cannot become protected ...

<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Shruthi Sam
Ranch Hand

Joined: Jul 09, 2008
Posts: 73
Thanks Harshit.. I understood.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Harshit Rastogi wrote:
For eg, in your case the protected method can be overriden with public and protected modfier. if you put private, compile time error will be there

Even can't extends class X since it is in different package
Shruthi Sam
Ranch Hand

Joined: Jul 09, 2008
Posts: 73
you can import the package and extend the class. It will work.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Shruthi Sam wrote:you can import the package and extend the class. It will work.


for that class needs to be public
Shruthi Sam
Ranch Hand

Joined: Jul 09, 2008
Posts: 73
yes it should be. Sorry, i forgot to mention that.
vvv gupta
Greenhorn

Joined: Jun 03, 2008
Posts: 4

Yes Shruti
while overriding the method the scope/access of the method cannot be more restricted.
This is the rule for overriding.

Subclass can add to funcnality whtever it recived from base class It cannot retrict its behaviour.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access modifiers