Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

covariant data types and overloaded method

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI
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
SuperCafe4Java
SuperCafe4Java
could you please explain??
<br>

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"));
}
}
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic