File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected access error

 
Thiru Thangavelu
Ranch Hand
Posts: 219
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ali Macky
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need to create an object of the base class. You can access the users variable directly.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic