wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Protected access modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Protected access modifier" Watch "Protected access modifier" New topic
Author

Protected access modifier

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477




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

Joined: Jan 24, 2006
Posts: 802
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 ]

You down with OOP? Yeah you know me!
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 05, 2006
Posts: 477
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

Joined: Feb 07, 2005
Posts: 2367
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.
 
subject: Protected access modifier
 
Similar Threads
exam lab gc question
Protected
Array Question
Possible error in Master Exam...
Garbage collection explanation