Reputation: 157
I have the following two piece of code :
/**
*
*/
package com.akshu.multithreading;
/**
* @author akshu
*
*/
public class MyThread extends Thread {
protected int b;
private int a;
@Override
public void run() {
super.run();
System.out.println("int a:"+a);
}
}
-----------
package com.akshu.utility;
import com.akshu.multithreading.MyThread;
public class MyUtility extends MyThread{
public static void main(String args[])
{
MyThread th1 = new MyThread();
int d =th1.b; // line1
System.out.println("int d"+d);
}
}
with the above files of code i am trying to understand purpose of protected access modifier. In the file MyUtility , I am trying to refer variable b of class MyThread.But its giving me below error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field MyThread.b is not visibilty.
My concern is variable b should be accessible from subclass as i have already extended the Mythread. But it is giving me compile time error. Also when i declare this variable as static in my superclass i was able to access it directly .So what wrong i am doing when i am trying to access via instance?
Upvotes: 2
Views: 486
Reputation: 148
The method main is not explicitly part of MyThread - if you would implement another function, e.g. prtintB(), you could use the direct access with the "." operator. To access it from main you have to write a getter function.
Upvotes: 2
Reputation: 36401
Java lang specification section 6.6.2.1 will tell you the truth :
If the access is by a field access expression
E.Id
, whereE
is a Primary expression, or by a method invocation expressionE.Id(. . .)
, whereE
is a Primary expression, then the access is permitted if and only if the type ofE
isS
or a subclass ofS
.
Here MyThread
is C
, MyUtility
is S
and b is Id
. So in a MyUtility ionstance you cannot use a reference to an instance pf MyThread
to access its b
Upvotes: 1
Reputation: 22181
From Kathy Sierra's great book, explaining the misunderstanding of protected
scope:
But what does it mean for a subclass-outside-the-package to have access to a superclass (parent) member? It means the subclass inherits the member. It does not, however, mean the subclass-outside-the-package can access the member using a reference to an instance of the superclass. In other words, protected = inheritance. Protected does not mean that the subclass can treat the protected superclass member as though it were public. So if the subclass-outside-the-package gets a reference to the superclass (by, for example, creating an instance of the superclass somewhere in the subclass' code), the subclass cannot use the dot operator on the superclass reference to access the protected member. To a subclass-outside-the-package, a protected member might as well be default (or even private), when the subclass is using a reference to the superclass. The subclass can see the protected member only through inheritance.
Thus, in your case, you try to use a reference to access a protected member outside parent's class package:
MyThread th1 = new MyThread();
int d =th1.b; //b cannot be reached !
Upvotes: 2
Reputation: 8976
You can't access protected properties from an instance. You can only access them in inheriting class. In this line-
MyThread th1 = new MyThread (); int d = th1 . b ;
you are actually trying to access a protected property from an instance th1
.
Upvotes: 2