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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "protected modifier" Watch "protected modifier" New topic
Author

protected modifier

Ashwin Sharma
Greenhorn

Joined: Nov 03, 2006
Posts: 28
hi all,
When you inherit some class(It has the protected variable) in same package, instance of sub class is able to access protected variable of other instance of same class.
But if you inherit the class in other package, instance of sub class is unable to access protected variable of other instance of same class.
Why is this so?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,


Ashwin Sharma posted November 13, 2006 01:51 AM
But if you inherit the class in other package, instance of sub class is unable to access protected variable of other instance of same class.

There should be no problem with instances of the same class extending the class with the protected member.

But the protected members are invisible in other classes extending the same class with the protected member.

e.g.
one file:


other file:


Therefore line A will compile, line B will not (protectedString not visible).


Yours,
Bu.
[ November 13, 2006: Message edited by: Burkhard Hassel ]

all events occur in real time
Ashwin Sharma
Greenhorn

Joined: Nov 03, 2006
Posts: 28
hi Burkhard,

May be i was'nt able to explain my doubt more clearly. So let me explain it again-
package pac1;
class Super
{
protected int a;
}

package pac1;
class Sub
{
public int b;
void method()
{
Sub s = new Sub();
b = s.b;
}
}

// the above will work.

package pac1;
class Super
{
protected int a;
}

package pac2;
class Sub
{
public int b;
void method()
{
Sub s = new Sub();
b = s.b;
}
}
// this code will not work.
Access specifier works at class level, so why does it shows this type of behaviour at the instance level.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Ashwin!

Please do not post the same question in multiple forums!


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: protected modifier
 
Similar Threads
A question regarding the protected member access
protected modifier
Second page of book, first question - Access modifiers
Section 6.6.7 of JLS ?
Protected variables access