Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

protected field

 
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
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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
 
Author
Posts: 114
11
Spring Tomcat Server Java
  • 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.


 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic