• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected access error

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic