File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes covariant data types and overloaded method 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 "covariant data types and overloaded method" Watch "covariant data types and overloaded method" New topic

covariant data types and overloaded method

sudharshan tettu
Ranch Hand

Joined: Jul 17, 2006
Posts: 126
Output of the following is different from what i expected.

even though subcafe4java has a get method which accepts String as a parameter
output is
could you please explain??

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"));
Rafael Souza

Joined: Jul 03, 2007
Posts: 6
Let's see...

First, the idea of covariant data type on return exists only when you are overriding methods. If you have two methods with different types of arguments, the two methods are different ones... as you said, its an overloading. So, it doesnt matter if String extends Object... they are not covariant in this context.

Now what do we have:

SuperClass: Object get (Object){return "super";}
SubClass: String get (String){return "sub";}

SuperClass superC;
SubClass subC;

When we have overriding, through polimorfism, at runtime the code that will run is that of the type of the actual object. But as we have an overloading, at runtime the code that will run is that of the type of the variable. So, as the type of the variable is SuperClass, what runs is the version of SuperClass get() method.

Now let's make some adjustments at your code to make things more complicated:

if the type of the argument in subclass was Object, then we would have overriding and you try to run the same code with new adjustments, you may note the same results would be:

superclass version
subclass version
[ July 04, 2007: Message edited by: Rafael Souza ]
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
You're have overloaded the Object get (Object o) of the SuperCafe4Java with the method Object get(String 0).

So the compiler will choose the method on reference type and not on object type.

Covariant data types is about return types and not argument types. The following code behaves as you expect.

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
I agree. Here's the link:
subject: covariant data types and overloaded method
It's not a secret anymore!