The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

protected access specifier allows access if...

Posted by Randall W. Smith on July 25, 2001 at 12:53 PM

I'm not sure I understand the original question but I thought that indeed you could access protected data member from another package IF you are using Inheritance. It makes the data memeber in question "friendly" to the derived class. At least that is my understanding. -RWS

>
> You can not access protected data element directly in different packages.

>
> > package Greek;

> > class Alpha {
> > protected int iamprotected;
> > protected void protectedMethod() {
> > System.out.println("protectedMethod");
> > }
> > }

> >
> > //////

> > import Greek.*;

> > package Latin;

> > class Delta extends Alpha {
> > void accessMethod(Alpha a, Delta d) {
> > a.iamprotected = 10; // illegal
> > d.iamprotected = 10; // legal
> > a.protectedMethod(); // illegal
> > d.protectedMethod(); // legal
> > }
> > }

> > Q.How can i understand above thing?
> > I think that a.iamprotected = 10; // legal and a.protectedMethod(); // legal

> >
> > Thanks.






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us