• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Covariant

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

'Sub1' class extends 'Base' class and 'Sub2' class extends 'Base' class. getValue() in Class B and Class C gives error. Is it Covariant?

 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java supports covariance this way:

In the code you posted the parameters of the sub classes don't match the parameters of the parent class and java sees them as unrelated methods with an overloaded name so it does not work.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kri shan wrote:'Sub1' class extends 'Base' class and 'Sub2' class extends 'Base' class. getValue() in Class B and Class C gives error.


Return types can be covariant, but argument types cannot.

The getValue() method in class B does not override the getValue() in class A correctly because in class B it takes a Sub1, while in class A it takes a Base.

This example explains why this is so:

 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:

kri shan wrote:'Sub1' class extends 'Base' class and 'Sub2' class extends 'Base' class. getValue() in Class B and Class C gives error.


Return types can be covariant, but argument types cannot.

The getValue() method in class B does not override the getValue() in class A correctly because in class B it takes a Sub1, while in class A it takes a Base.

This example explains why this is so:



As a side note; apparently; Sather supports contravariant but I am not sure if it is what the OP was trying to do but it looks similar...
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not think contravariance is relevant to the question here. That link however suggests that C# supports covariance, contravariance and invariance in generics.
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic