Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pelase Help me out imme....

 
deepu Bhalotia
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {String s1 = "C";}
class D {
static void m1(A x) {System.out.print(x.s1);}
static void m1(B x) {System.out.print(x.s1);}
static void m1(C x) {System.out.print(x.s1);}
public static void main(String[] args) {
A a; B b; C c; a = b = c = new C();
m1(a); //1
m1(b); //2
m1(c); //3
}}


// Hi Guys,
Can you pleas explain me .... in line 1,2 and 3 which method will be called.

Thanks,
Deepak
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A {String s1 = "A";}
class B extends A {String s1 = "B";}
class C extends B {String s1 = "C";}
class D {
static void m1(A x) {System.out.print(x.s1);}
static void m1(B x) {System.out.print(x.s1);}
static void m1(C x) {System.out.print(x.s1);}
public static void main(String[] args) {
A a; B b; C c; a = b = c = new C();
m1(a); //1
m1(b); //2
m1(c); //3
}}




The output will be A B C , as it is a compile time binding which depends on the type of reference , not on the actual object .
 
deepu Bhalotia
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know the Output...
My Question is ...

which overloaded method will be called by
m1(a)....m1(b), m1(c)..statements...


Thanks
Deepak
 
Ramesh Ch
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

m1(a) called the m1(A x)
m1(b) called the m1(B x)

m1(c) called the m1(C x).

Thanks
 
Soni Prasad
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


line no. 1 -> m1(A x) line no. 4
line no. 2 -> m1(B x) line no. 5
line no. 3 -> m1(C x) line no. 6
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic