• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing members of an inner class

 
venkat paruchuri
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
I got a situation, how to access the memebers of protected inner class from another package.
The exact problem is, in a package1 there are three classes and one interface.
For example, in a package1,
ClassA has protected inner class called ClassB.
ClassB has protected inner class called ClassC.
ClassB implements interface called InterfaceA.
I have a class called ClassD in an other pacakge called package2, which has to access the public members of the protected inner class ClassC in package1.
pictorically,
package1;
class ClassA {
protected class ClassB implements InterfaceA {
protected class ClassC{
}
}
}
package2;
class ClassD{
how to access memebers of ClassC in package1?
}
I appreciate if any body can help me to overcome this problem. Thanks in advance..
[ February 15, 2002: Message edited by: venkat paruchuri ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, access issues aside, first you can't access any members of the inner Classes, you can only access members of instances of those inner classes. So you are talking about creating new instances of those inner classes, then accessing the members of those instances.
Now since these inner classes are protected, it means that ONLY classes in the current package, OR subclasses of class 'A' in any package, may access those inner class members.
Since D is neither in the same package, nor is it a subclass of 'A', class 'B' and class 'C' are not accessible to class D.
Basically, you can't access the members of either class from any methods/statements in D.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words, the whole idea of "protected" access is to prevent you from doing what you want to here.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AFAIK there might be a way to do this using reflection.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so. The following code throws an IllegalAccessException at the getInt() line:

However it works fine if you change the declaration of value from private to public. There are probably other ways to try this using reflection, but I imagine all reflective methods will eventually encounter the same sort of problems. I'd certainly be interested in hearing about it if there's a way to do this...
[ February 17, 2002: Message edited by: Jim Yingst ]
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output:
private int Hider.value
value = 17

[ February 17, 2002: Message edited by: Mapraputa Is ]
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, it is possible.
Here is victimized code:

Here you can get ObjectInspector class that output passed object's state via Reflection.
I get an instance through a public class Secret1 from the same secrets package
Object sc = Secret1.getSecret();
And then called ObjectInspector:
ObjectInspector inspector = new ObjectInspector ();
inspector.output (secret, new FileWriter ("inspect.html"));
output:
secrets.Secret#0
hidden1 int 42
tp secrets.Secret$TopSecret$TopestSecret secrets.Secret$TopSecret$TopestSecret#1
secrets.Secret$TopSecret$TopestSecret#1
this$1 secrets.Secret$TopSecret secrets.Secret$TopSecret#2
hidden int 17
secrets.Secret$TopSecret#2
this$0 secrets.Secret secrets.Secret#0

[ February 17, 2002: Message edited by: Mapraputa Is ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, good catch. I didn't know about setAccess(). Cool, thanks.
 
venkat paruchuri
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

Thank you very much for your help. I got a clue in my problem. I found a method in InterfaceA which will return an another interface object that is implemented by ClassC. That returned interface object being public object I got access from ClassD in package2. so, my problem is solved.
Once again thanks to all of you for your response.
venkat
[ February 25, 2002: Message edited by: venkat paruchuri ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic