Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Classes and Super Classes

 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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).
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic