0

As per my understanding rules and access level of protected variable :
If same package

class A {    

protected int =200;    

}    

class B extends A {    

B is ref variable of B
B.i overriding the value of i in class B

b.i = 400;    

a is ref variable of A
a.i will always print 200

} 



class C extends B {    

c is ref variable of C c.i overriding the value of i in class C

c.i = 500;  

a is ref variable of A a.i will always print 200

}    

Conclusion : Protected member of class A is public in same package for all the classes.

if different packages use A
Here A and D is not in same package

class D extends A {     

System.out.print(a.i);    

Above statement will throw compile time error
Here a.i is not accessible, i is now private in A
You can use i only with ref variable of D

System.out.print(d.i);    
//no error

}

class E extends D {    
System.out.print(a.i);    

Above statement will throw compile time error
Here a.i is not accessible, i is now private in A

System.out.print(d.i);    

Above statement will throw compile time error
Here d.i is not accessible, i is now private in D You can use i only with ref variable of E

System.out.print(e.i);  

No error

} 

Conclusion : Protected member of class A is behaving as private variable in other packages for its immediate sub class.

Can anyone give me more details on access level of protected variables ?

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
KP_JavaDev
  • 222
  • 2
  • 4
  • 11
  • Just... some random details? Apart from not being about an actual problem, your question is too broad. – Marko Topolnik Jul 26 '15 at 10:59
  • Marko - my question is very simple :) i just wanted to know the access level for protected variables packages wise. – KP_JavaDev Jul 26 '15 at 11:05
  • If your question is simple, then clearly state it. `Can anyone give me more details on access level of protected variables ?` is _not_ a clear and simple question, it is a general request for unfocused contributions. – Marko Topolnik Jul 26 '15 at 11:08

2 Answers2

2

Have you tried official Java tutorial, which gives quite a decent explanation of protected modifier usage?

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.

Or, if you had and you still got some questions, pls specify them a little more

Community
  • 1
  • 1
solar
  • 1,344
  • 1
  • 8
  • 14
  • Solar - Thanks for the official java tutorial link. My question is why protected members are private in sub class of another packages. we can only access protected variables in sub class by ref variable of sub class not by ref variable of actual class. – KP_JavaDev Jul 26 '15 at 11:32
  • well, you can use parent's protected variable by **direct** access to it from child class (even if these classes doesn't share the same package), that's one of the inheritance aspects. When you're accessing your variable like this: `System.out.print(a.i); ` you're doing it wrong. Either you should make your variable static if you need to use it by parent's reference for some reason: `System.out.print(A.i); ` or just use **this** keyword like `this.i` Did I answered your question? – solar Jul 26 '15 at 12:19
0

Read this link you will find your answer.

https://stackoverflow.com/a/19949375/5152910

Either create some getter and setter to use the variable in other subclasses or you this.i to access the variable i.

Community
  • 1
  • 1
insa_c
  • 2,851
  • 1
  • 16
  • 11