• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question from danchisholm

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 431
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
It was the best of times. It was the worst of times. It was a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic