wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Access to protected variable of  class in one package from other package 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 "Access to protected variable of  class in one package from other package" Watch "Access to protected variable of  class in one package from other package" New topic
Author

Access to protected variable of class in one package from other package

giri babu
Greenhorn

Joined: Jun 07, 2000
Posts: 4
Hi all,
i am new to this group.
I have a question
i have a package "A" in which "ClassA" has a protected variable
"X".
now i created another package "B" in which ClassB extends ClassA
now i created Object for ClassB and accessed the variable X of Class it works fine as i extended ClassA.
now i created Object for ClassA i try to Use the variable X with Object for ClassA i am unable to do so why?
(this happens in only case Diffpackege=>extended=>parentobject=>protected variable accesed)
can anybody through some light on this issue and provide me some explanation i aill be very glad..

Best Regards<BR> Giri
John Wetherbie
Rancher

Joined: Apr 05, 2000
Posts: 1441
I would probably help to post your code to illustrate what you are doing. The only thing I can think of based on your description is that you trying to access X from a scope where this is not allowed.


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
giri babu
Greenhorn

Joined: Jun 07, 2000
Posts: 4
//first source file
package test;
public class Parent1{
protected int x=10;
public static void main(String args[]){

}
}
//*****************************
//second sourcefile
package best;
import test.Parent1;
public class child extends Parent1{
public void sub(){
System.out.println("in sub"+x);
}

public static void main(String args[]){

child ch=new child();
ch.x=1;

Parent1 p=new Parent1();//creating parent class object
System.out.println("child object ch:="+ch.x);
System.out.println("child object ch1:="+p.x); //This line giving me the error
ch.sub();
}
}

I hope this code will help you to explore the problem.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Originally posted by giri babu:
//first source file
package test;
public class Parent1{
protected int x=10;
public static void main(String args[]){

}
}
//*****************************
//second sourcefile
package best;
import test.Parent1;
public class child extends Parent1{
public void sub(){
System.out.println("in sub"+x);
}

public static void main(String args[]){

child ch=new child();
ch.x=1;

Parent1 p=new Parent1();//creating parent class object
System.out.println("child object ch:="+ch.x);
System.out.println("child object ch1:="+p.x); //This line giving me the error
ch.sub();
}
}

I hope this code will help you to explore the problem.

Actually u r accsssing the protected data member of a class that is in diffrent package if it is in same package than u may accsses it.Acssess specifier is protected sop it will be local to that package only. Make it public than accsses by following code

package test;
public class Parent1{
public int x=10;
public static void main(String args[]){
}
}
import test.Parent1;
public class child extends Parent1{
public void sub(){
System.out.println("in sub"+x);
}
public static void main(String args[]){
child ch=new child();
ch.x=1;
Parent1 p=new Parent1();
System.out.println("child object ch:="+ch.x);
System.out.println("child object ch1:="+p.x);
ch.sub();
}
}
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
I don't have the book with me now but in page sixty-something of THE JAVA PROGRAMMING LANGUAGE by Ken Arnold there's a section titled "What protected really means".
It took me more than one read to understand it but I finally did
In short, outside of the parent class package, a protected field is available only through a reference type of the subclass (or any of its subclasses).
Going back to the book, a new edition has just been published:
Paperback - 624 pages 3 edition (June 2, 2000):
The Java Programming Language, Third Edition

Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
And last but not least, the long anticipated (by me at least!)
The Java Language Specification, Second Edition
has been published this month also:
Paperback - 544 pages 2 edition (June 2, 2000)
Tony Alicea
Desperado
Sheriff

Joined: Jan 30, 2000
Posts: 3222
    
    5
Hmmm... I just noticed we don't have a "Books" section for people to comment, recommend them etc.
Harry Chawla
Ranch Hand

Joined: Jun 03, 2000
Posts: 97
Hi Tony
How good is the book 'Just Java 2' as compared to 'THinking in Java' for clien RAD programmes with no real exposure to Oops programming and java. More so because TiJ is available in year 2000 edition where as for Just Java 2, only june 1998 edition is available.
[This message has been edited by Harry Chawla (edited June 11, 2000).]
bharati
Greenhorn

Joined: Apr 29, 2000
Posts: 14
Class Member Access
--------------------
Protected
same class yes
same package yes
subclass
same package yes
non sub-class
Different Package yes
subclass
Different package no
non-subclass
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
But protected data member may be accessed anywhere in the subclass whereever subclass may be(even if it is in different
package) is it right...
Can some one clearly explain the protected data member access
plez.


Bhasker Reddy
Paul Caudle
Ranch Hand

Joined: Jun 08, 2000
Posts: 64
This is a little tricky. The general rule of thumb is that protected members may be accessed from classes in the same package as well as subclasses of itself. This is not quite as explicit as it needs to be.
To expand on that, protected has the same access as "Friendly" (default) and in addition a protected member can be accessed within its subclass but only of the current object.
It is not possible to access a protected member from outside the package which contains the superclass if you are dealing with a separate superclass object, PERIOD.
It is possible to access this protected member from outside the package which contains the superclass iff you are dealing with the current subclass object(which has inherited the protected member).
public class BB extends someOtherPackage.AA {
public BB(){
//you can use the current object to access the
//protected member
this.protectedMember = 3;//LEGAL
//OR
super.protectedMember = 3;//LEGAL
//but can't access the protected member of another object
AA a = new AA();
a.protectedMember = 4;//ILLEGAL
}
}
Hopefully that should shed a little light on things.
-Paul
giri babu
Greenhorn

Joined: Jun 07, 2000
Posts: 4
Thanks paul now i understood the access clealy.
Thanks&Regards
Giri
 
Don't get me started about those stupid light bulbs.
 
subject: Access to protected variable of class in one package from other package
 
Similar Threads
"=" oprator question
question about "has a" at SUN's mock exam
NullPointer exception
Save objects problem
Why use protected???