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 doubt

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I guess answer would be
SubCafe4Java
SubCafe4Java
But when i run the programme it shows
SuperCafe4Java
SuperCafe4Java

We are calling
superFoo.get("super") i.e., superclass reference with subclass object in it, also parameter "super" is a String object. So it is more appropriate to call subclass get() but why superclass get() called?

I can't understand.... thanks in advance for any help...
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you are redifining the function in the child class you are not overloading the base class function, because the base class is having parameter as Object but child class is having parameter as String. So it is not covariant return.

and when you are calling the funciton you are calling using the base class version. so always function of the base class is getting called
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where are the covariant retrun types?
I think you got confused and pretends to think that the methods are overridded.Infact the methods are overloaded in first two classes.To make it a valid overrided method with covariant return type make change as
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


2nd one can't override 1st because the argument signature is different even thought String is subclass of Object, hence it's called overloaded - name reuse but new method.



Now superFoo owns only the 1st method, it's always legal - here the argument Object is subcast to String again when get("super") being called.

finally it prints out "SuperCafe4Java";
 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot to all.
 
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