• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

regarding "protected" modifier

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,

As far as i know , protected memebers can only be accessed from inside the class or by a class which is inheriting it , but here i am able to call a protected member from outside a class & the irony is the second class is not inheriting it .

Please explain me where am i wrong ?

Here is the code

class visible
{
protected void dis()
{
System.out.println("In dis visible");
}


}
______________________________________________________________________
Now

class test
{

public static void main(String arg[])
{
visible b = new visible();
b.dis();
}


}

o/p is
In dis visible (WHY )
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
protected members can also be accessed from classes in the same package.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
they can also be accessed from outside the package if u inherit. and within the package u can access it using composition.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Default (aka package) scope makes members visible to classes in the same package. Protected makes them visible within the package plus classes that extend the containing class. You'll find many sources that only mention the extending classes and forget to remind us about the classes in the same package. Bruce Eckell keeps it all straight in Thinking In Java.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic