aspose file tools*
The moose likes Java in General and the fly likes Inner Classes and Super Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Inner Classes and Super Classes" Watch "Inner Classes and Super Classes" New topic
Author

Inner Classes and Super Classes

Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
I have this inner class called InnerClass that is inside of OuterClass. InnerClass extends InnerBase and OuterClass extends OuterBase. There is a variable (public int myInt) that is inside of OuterBase. How do I get to it from InnerClass? Can this be a one liner. My book says it is something like Outer.super but I can't seem to get my compiler to accept it. See if you can get it?
Dale


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
I thought all the member variables of the enclosing class were available, without any prefixes, in an inner class. This should apply equally to the member variables that come from the outer class's superclass.
You'd only have problems if the names were not unique. Also, your inner class needs not to be declared static.
... but I don't have a JDK installed here to check.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
They would be available if I didn't make the variables private. But if I make the variables private, then I can only get to the variables through an instance of the superclass. So the question still stands...
-Dale
[ February 12, 2002: Message edited by: Dale DeMott ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
> There is a variable (public int myInt) that is inside of OuterBase
Later you're saying it might be private. So I'm a bit confused. If you make it private, then it will only be available inside OuterBase. You've said that InnerClass is inside OuterClass, which extends OuterBase. But it sounds like InnerClass is not inside OuterBase - it's only inside OuterClass. If you're not inside OuterBase, you will never, ever be able to access a private variable of OuterBase. It doesn't matter that OuterClass extends OuterBase - private variables are not accessible from subclasses. (Unless the subclass is also defined as an inner class within the base class - but that's a horrid design that's only useful for creating tricky questions, not for doing anything productive in the real world.) If you need to access it, make it accessible. Or better yet, give it an accessible get() method (and possibly set() if you need it).


"I'm not back." - Bill Harding, Twister
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
You are correct in the fact that you cannot get to the superclass' members if they are private. The question now stands, if you are in the innerclass can you get to the superclass of the outerclass' public members. (Assuming you want to explictly print out the Outerclass' variables)
I'm studying for my cert and wonder if this is possible or not.

-Dale
[ February 13, 2002: Message edited by: Dale DeMott ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
In the code you show, it is sufficient to simply type

since vairable ob is public and has not been hidden by another variable with the same name in a subclass or inner class. If you want to be more explicit:
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
This isn't accepted by the compiler. It expects a ( between the super and the ob. Hmm.. does this compile on yours?
-Dale
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It compiles and executes fine for me using J2SE SDK 1.3.1 and 1.4.0-rc, on Windows 2000 and NT. Also the JLS describes this invocation mechanism here. What JDK are you using, and exactly what does the message say?
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
I'm using Visual Age for Java 3.5.3. It says it is expecting a ( and highlights here where the ^ sign is. Very Odd. I thought the answer would be what you said as well. That is why I was perplexed when it told me I had an error.
System.out.println(OuterClass.super^.ob);
-Dale
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Sounds to me like a bug in VAJ. Try compiling from the command line using a standard JDK to be sure. Then report the bug to IBM. I couldn't find a bug list for VAJ online, and I don't know what sort of support IBM actually provides. I assume this isn't actually a crucial issue for you - but it's nice to think that if you point the problem out, they might fix it in the future. Good luck.
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Yeah. I agree that this is probably a bug in VAJ. My Osborne Sun Certified Programmer for Java 2 Book states what you said to be true. This is why this question arose.
Regards
-Dale
 
wood burning stoves
 
subject: Inner Classes and Super Classes