aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected access error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected access error" Watch "protected access error" New topic
Author

protected access error

Thiru Thangavelu
Ranch Hand

Joined: Aug 29, 2001
Posts: 219
I have the superclass like this.
package payroll;
public class UserConnection {
protected int users = 0;
}
and the subclass
package client;
import payroll.UserConnection;
class ServerConnection extends UserConnection {
public UserConnection getUserConnection() {
UserConnection u = new UserConnection();
u.users += 1;
return u;
}
}
why the subclass is not compiling saying users variable is not accessible? I have imported superclass and the variable users is protected which means it is accessible in subclass isn't? What is the error here or how to make the subclass to work properly?
Thanks.


Thanks,<br />Thiru<br />[SCJP,SCWCD,SCBCD]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
This topic was just covered in this thread. The skinny of it is that you can't access that member because you're not using it in code that is responsible for the implementation of that class.
Check out the JLS, §6.6.2.1 Access to a protected Member for more details.
Corey


SCJP Tipline, etc.
Ali Macky
Greenhorn

Joined: Mar 28, 2002
Posts: 8
You do not need to create an object of the base class. You can access the users variable directly.


[i]Ali[i]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Ali Macky:
You do not need to create an object of the base class. You can access the users variable directly.

Creating a new instance of the UserConnection class and modifying its users member is very different from modifying your own, inherited, users member. Be sure you know the difference.
Corey
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected access error