• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Pelase Help me out imme....

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic