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

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

 
giri babu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
John Wetherbie
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
giri babu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... I just noticed we don't have a "Books" section for people to comment, recommend them etc.
 
Harry Chawla
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Caudle
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks paul now i understood the access clealy.
Thanks&Regards
Giri
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic