Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding java's protected modifier

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 int protectedInt = 1; 

Here is the code for class A

package package1;  public class A {      public int publicInt = 1;     private int privateInt = 1;     int defaultInt = 1;     protected int protectedInt = 1;  } 

And here is the code for class C:

package package2; import package1.A;  public class C extends A{      public void go(){         //remember the import statement         A a = new A();         System.out.println(a.publicInt);         System.out.println(a.protectedInt);      } } 

Eclipse underlines the last line in C.go() and says "A.protectedInt" is not visible. It seems that this conflicts with the definition of the "protected" keyword, given in the oracle documentation.

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

What's going on here?

like image 211
mahela007 Avatar asked Sep 02 '13 12:09

mahela007


People also ask

How does a protected access modifier work?

Protected Access Modifier - Protected Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. The protected access modifier cannot be applied to class and interfaces.

Why protected modifier is used?

Protected Access ModifierA member is declared as protected as we can access that member only within the current package but only in the child class of the outside package.

What are the 4 access modifiers?

Simply put, there are four access modifiers: public, private, protected and default (no keyword). Before we begin let's note that a top-level class can use public or default access modifiers only. At the member level, we can use all four.


2 Answers

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); 
like image 109
Jon Skeet Avatar answered Oct 14 '22 01:10

Jon Skeet


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

like image 44
sanbhat Avatar answered Oct 14 '22 01:10

sanbhat