aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why this output? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why this output?" Watch "why this output?" New topic
Author

why this output?

Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

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


Thanks, Shiv
SCJP, OCE - JSP & Servlets Developer
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
What do you think you are doing? Overriding or Overloading?


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
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

Joined: Sep 13, 2006
Posts: 126

Thanks Barry Gaunt. I got the point
Guru dhaasan
Ranch Hand

Joined: Sep 13, 2006
Posts: 126

Thanks Nik. You've cleared my doubt.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why this output?