• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why this output?

 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class SuperCafe4Java {
public Object get (Object o) {
return ("SuperCafe4Java");
}
}

class SubCafe4Java extends SuperCafe4Java {
public Object get (String o) {
return ("SubCafe4Java");
}
}

class TestCafe4Java {
public static void main (String[] arguments) {
SuperCafe4Java superFoo;
SubCafe4Java subFoo;

superFoo = new SubCafe4Java();
System.out.println (superFoo.get("super"));

subFoo = new SubCafe4Java();
superFoo = subFoo;
System.out.println (superFoo.get("super"));
}
}


When run, this produces the following output:
SuperCafe4Java
SuperCafe4Java


If the invocation of object is determined at runtime, why it is giving the output.....

we are creating the sub class objects but why it is calling superclass method ???
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think you are doing? Overriding or Overloading?
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guru,
The arguments of the method are different its overloading.In overloading which method should be called is decided by reference type not on object type. So dont get confused between overriding and overloading.



Regards
Nik
SCJP 1.5
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry Gaunt. I got the point
 
Guru dhaasan
Ranch Hand
Posts: 126
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nik. You've cleared my doubt.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic