• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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: 76802
366
  • 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.
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic