Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected access modifier

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




The protected modifier allows access to any class in the same package AND any subclass in other packages.

But why the line 1 and line 2 are showing compile error?Why Java_StudyNotes instance won�t access a(instance variable) and testit()(method).
Please help me.I am trying to understand Protected modifier.

Thanks in advance.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can't use another classes protected modifiers, unless you extend from

that class...

A protected modifier means that any child class can use it, not just any

class or sub - class, and for it to become a child class you have to

extend or implement..

and i'm not sure if you can just extend from any class, i think you have

to extend from an abstract class, and implement an interface....

make your first class abstract, extend from it in the second one, and

then everything should work...

for example:


Monk...
[ April 17, 2006: Message edited by: Monk Fox ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Monk,

Thanks for your reply.I have read
" The default modifier ("friendly") allows access to any class (or subclass) in the same package.
The protected modifier allows access to any class in the same package AND any subclass in other packages. "

Still,i am cofused with access modifiers Protected and Default access.

Can anyone clear my doubt with any coding example.Please....
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example, there is no "is a" relationship between the classes. You cannot access a variable of Java using an instance of Java_StudyNotes.

This doesn't have anything to do with the access of protected variables.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keith,

Thankyou for the explanation.So,protected class members(instance variable and methods)can be accessed in extended classes.A subclass can access the protected members of the super class,however other classes(not derived through inheritance or in the same package)cannot access the protected variable or protected method.Am i right?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiva Mohan:
Hi Keith,

Thankyou for the explanation.So,protected class members(instance variable and methods)can be accessed in extended classes.A subclass can access the protected members of the super class,however other classes(not derived through inheritance or in the same package)cannot access the protected variable or protected method.Am i right?


Yes, that is for the most part correct.

A class inherits protected members from it's superclasses.

Also classes in the same package can access protected members.

However, there are some subtleties about the use of protected that you need to look at and make up a few examples to make you understand it.

I refer you to the Java Language Specification where an example of protected access is discussed.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic