• Post Reply Bookmark Topic Watch Topic
  • New Topic

regarding "protected" modifier  RSS feed

 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • 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 )
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
protected members can also be accessed from classes in the same package.
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!