• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected field

 
Ankit Gareta
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am little confused about the inheritance of the protected field.



now i inherited it in different package...


now again in different package


so protected variable is available to the hierarchy of the inheritance tree ...

Thanks in advanced
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure the code you've shared here is the one you tried to run? It looks like you tried to create a package starting with the word "java" which is not allowed...
 
Ankit Gareta
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i made mistake first time.....

so, i changed the question
 
Alex Theedom
Author
Ranch Hand
Posts: 77
5
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ankit,

In the third code snippet I think you mean to import test2.* rather that test1.*. Anyway, the protected access modifier respects inheritance.

Note that in the following code snippets a compile time error occurs stating that The field Test1.val is not visible.

In the first snippet below the Test4 class is a "neighbour" of the Test3 class and in the second snippet the Test4 class is in a completely different package and therefore cannot access the protected variable.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic