Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question from danchisholm

 
deepu Bhalotia
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.danchisholm.net/july21/mybook/chapter11/exam1.html

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
}}

Can you explain me about line number-6...
 
S.L.Narayanan
Ranch Hand
Posts: 431
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi...This is quite simple...they are creating an object for inner class and they are calling a method present inside the inner class.

new A().new B() will create an object for B. So new A().new B().m1(); will invoke method m1().

Inside that they are printing 4 things...
(1). this.s1 which means s1 present in the class B..--->B
(2). super.s1 which means s1 in the class SuperB.--->SuperB
(3). A.this.s1 which points to s1 in the class A --->A
(4). A.super.s1 which points to s1 in the class SuperA--->SuperA

So output will be B, SuperB, A, SuperA. Clear???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic