Win a copy of Head First Android this week in the Android 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Protected Member Access Problem

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two classes as



and



Still i am getting Compilation error as x has protected access in p.Animal .. But Protected Members can be accessed From Subclasses Outside the Package, Then y i am geeting such error?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
protected members can accessed only through inheritance if the sub class is in another package.


Noe try it.
 
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



NOTE: You can use Animal reference to access protected member within package p, but not outside package p.
if you want to access x then just use x to access it or this.x. But mind it, static context cannot have reference to any non-static context. Thats
why in the above code (by Abimaran ) x is accessed through Dog object. The following code is also legitimate and in this code we would use just
x.





The above code just compiles well ,But in order to print the value of x, you have to define main(String[] args) method which would call main() method above
by the use of some instance of Dog class.

One more Tip : We can have main() method, this is an overloaded method and you would study it in later chapters.


NOTE: After grasping the above concept, try answering the following question, but before attempting it do not forget to read the rules given as a link in the same.

SCJP QUESTION OF THE DAY SERIES
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
read this post everything will be cleared..........

https://coderanch.com/t/502346/java-programmer-SCJP/certification/protected-variable
 
reply
    Bookmark Topic Watch Topic
  • New Topic