• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is a protected class member "inherited" by a subclass?

 
Ranch Hand
Posts: 56
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose you download a jar file, project-a.jar. You know that this jar contains a public class abc.PublicClass which you plan to use in your own project.

It happens that internally to the project-a.jar the abc.PublicClass extends a private package class abc.PackageHiddenClass which contains a protected field.

Here is the code for abc.PackageHiddenClass:

And here's the code for abc.PublicClass:


Now suppose in your own project you write code which creates a variable of type abc.PublicClass and you try to access the protected field:

I understand that it will not compile but the compiler message threw me off a little:

$ javac -cp deps/project-a.jar xyz/Test.java
xyz/Test.java:8: error: protectedField has protected access in PackageHiddenClass
   System.out.println(publicClass.protectedField);
                                 ^
1 error



I had this notion that a protected member is "inherited" by a subclass so I expected that the compiler will say that "protectedField has protected access in PublicClass".

Question: should I change the way I think about protected members of a superclass?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vyacheslav Belenky wrote:
Question: should I change the way I think about protected members of a superclass?



Not exactly sure what you mean ... except... it doesn't compile because of rule 6.6.2 of the JLS... https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2

... or basically, the TestClass is not responsible for the protected members of the PublicClass (and in different package), and hence, can't access it. This is regardless of whether the protected member is part of the PublicClass, or inherited from a superclass.

Henry
 
Stan Belen
Ranch Hand
Posts: 56
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Henry for the answer.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic