Reputation: 11106
I'm reading this Java SCJP book and I came across this:
The protected and default access control levels are almost identical, but with one critical difference. A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.
So I decided to test out the protected
point.
I have a super-class in a package
package scjp;
public class Token {
protected int age = 6; //This is the protected class-level variable.
public Token(String name){
this.name = name;
}
public Token(String name, int age){
this.name = name;
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
}
The I have a sub-class in another package;
package pack;
import scjp.Token;
public class son extends Token{
public static void main(String[] args) {
System.out.println(Token.age);
}
}
As you can see I'm trying to access the protected class-level integer variable age
in the super-class.
But I get this error:
age has protected access in scjp.Token
at pack.son.main(son.java:11)
Java Result: 1
So, what's going wrong?
Upvotes: 1
Views: 2890
Reputation: 5530
Protected Member : In other words, protected = Inheritance
For a subclass outside the package, the protected member can be accessed only through inheritance. The subclass cannot use dot operator on the superclass reference to access the protected member.
you're not accessing it from the subclass. You're accessing it from main().The "main" method isn't a member of either class, but it's trying to directly access the member variable.
public class son extends Token{
public static void main(String[] args) {
System.out.println(Token.age); // Error
}
}
Create non-static method and inherit the protected member.
public class son extends Token{
public void test1() {
System.out.println(age); // 6
System.out.println(new Token().age); // 6 This also works.
}
}
Upvotes: 0
Reputation: 1279
You can't access a static member, if you want to test inheritance you can do as following
public static void main(String[] args) {
son s = new son();
System.out.println(s.age);
}
You shouldn't use name of public class begin with a lowcase letter.
Upvotes: 0
Reputation: 15906
You are misunderstood the concept
whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.
Means you can access the member by extending it in your class but you are try to access variable in your class through static concept even your variable is not static So i think you have to take a look of basics java. Means while you can fix your program in two way.
1. create object of your class and access your protected variable for that you have to create a constructor which should call parent constructor . Your son class may be look like following .
public son(String name) {
super(name);
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
son s = new son("Hello"); //$NON-NLS-1$
System.out.println(s.age);
}
2. you can get access by making your protected variable as static and use it from your class like following.
Token class:
public class Token {
protected static int age = 6; //This is the protected class-level variable.
....
}
son class:
System.out.println(son.age);
Upvotes: 1
Reputation: 46428
age
is not a static variable in your class Token
. its an instance non-static member and you access instance non-static memebers using the instance of your class.
System.out.println(new Token().age);
should work
And also as your Son IS-A Token
you should be able to access it directly unless you have another variable with the same name(shadowing) in your Son class in which case it would access the 'age' declared in Son, as i can see you have not declared age
in your Son class you could also directly access it like this:
System.out.println(age);
Upvotes: 2
Reputation: 5837
You are trying to access a non static protected member of your super class with class reference..
public static void main(String[] args) {
Token t = new Token("somehting");
System.out.println(t.age);
}
will compile and work fine.
Upvotes: 3
Reputation: 66693
age
is not a static member. You will need to access it using an instance of the class Token
. Not the class name itself.
Upvotes: 1