I have a class called A in package1 and another class called C in package2. Class C extends class A.
A has an instance variable which is declared like this:
<Protected means :
a)This member will be accessible to all classes in same package through A object’s reference.
b) For different package, this will be accessible only inside Subclasses of A say B and the reference used can be of B instance or of any subclass of B.
Let's take an example:
Let A be parent class in some package say com.ex1
Let B ,C be classes in different package w.r.t to A say com.ex2
. Also, B extends A
and C extends B
.
We will see how we can use protected field of A inside B (a subclass of A)
A's code:
public class A {
protected int a = 10;
}
B's code:
public class B extends A {
public void printUsingInheritance() {
// Using this
System.out.println(this.a);
}
public void printUsingInstantiation() {
// Using instance of B
B b = new B();
System.out.println(b.a);
// Using instance of C as C is subclass of B
C c = new C();
System.out.println(c.a);
A a = new A();
System.out.println(a.a); // Compilation error as A is not subclass of B
}
}
C's code:
public class C extends B {
}
For protected Static :
Same rules apply except that in b) now it is accessible in any subclass of A by A's class reference. Reference
public void go(){
//remember the import statement
A a = new A();
System.out.println(a.publicInt);
System.out.println(a.protectedInt);
}
When you are doing A a = new A();
and a.protectedInt
you trying to access protected member of A which is illegal according to java standards
Instead you can do this.protectedInt
directly.
Within the same package where the protected member is declared, access is permitted:
package package1;
public class C extends A{
public void go(){
A a = new A();
System.out.println(a.protectedInt); // got printed
C c = new C();
System.out.println(c.protectedInt); // got printed as well
}
}
Outside the package where the protected member is declared, access is permitted if and only if by code that is responsible for the implementation of that object. In this case, C is responsible for the implementation of that object, so it could access the protected.
package package2;
public class C extends A{
public void go(){
A a = new A();
System.out.println(a.protectedInt); // compiler complains
C c = new C();
System.out.println(c.protectedInt); // got printed
}
}
No need to instantiate Protection class inside Protection2 Class. You can directly call the protected variable without instantiating the Protection class. Because Protection2 class extends Protection class. So variable automatically inherited by subclass.
Try with below code:
public class Protection2 extends Protection{
Protection2()
{System.out.println("n_pro = " +n_pro);
}}
Since C
is inheriting A
, C
can directly use the protected
variable of A
like below
public class C extends A{
public void go(){
System.out.println(protectedInt);
}
}
As per your code, you are creating an instance of A
and accessing protected
variable through that instance, which violates java's rule - A protected variable is not visible outside the package
What's going on here?
You've misunderstood the meaning of protected
. You can access the protected members declared in A
from within C
, but only for instances of C
or subclasses of C
. See section 6.6.2 of the JLS for details of protected access. In particular:
Let C be the class in which a protected member is declared. Access is permitted only within the body of a subclass S of C.
In addition, if Id denotes an instance field or instance method, then:
[...]
If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.
(Emphasis mine.)
So this code would be fine:
C c = new C();
System.out.println(c.publicInt);
System.out.println(c.protectedInt);