• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing enclosing class' super class in inner

 
prafulla shrikhande
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you access enclosing class' super class variables.
I know about <outer>.this.<variable>
Is there such a thing like <outer>.super.<variable>

Look at the code below -

class SuperA {String s1="SuperA";}
class SuperB {String s1="SuperB";}
class A extends SuperA {
String s1="A";
class B extends SuperB { // 1
String s1="B";
void m1() {
System.out.print(this.s1 + ","); // 2
System.out.print(super.s1 + ","); // 3
System.out.print(A.this.s1 + ","); // 4
System.out.print(A.super.s1); // 5
}
}
public static void main (String[] args) {
new A().new B().m1(); // 6
}}

What is the result of attempting to compile and run the program?

a. Prints: B,SuperB,B,SuperB
b. Prints: B,SuperB,A,SuperA
c. Compile-time error at 1.
d. Compile-time error at 2.
e. Compile-time error at 3.
f. Compile-time error at 4.
g. Compile-time error at 5.
h. Compile-time error at 6.
i. Run-time error
j. None of the above
 
Joe Borderi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried compiling and running the program? In Java you unequivocally should get the same result I do.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, to answer such a question as Is there such a thing like <outer>.super.<variable>? you can compile the code to find out. (Took 30 seconds to do so ).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic