Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8888029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:56:56+00:00 2026-06-14T21:56:56+00:00

I just learned in my programming languages class that contravariant argument types would actually

  • 0

I just learned in my programming languages class that “contravariant argument types would actually be safe, but they have not been found useful and are hence not supported in practical languages.” Even though they are not supported, I am confused as to why something like this example we were given would still be, in theory, “safe”:

class Animal {
  ...
  public bool compare(Panda) { ... }
} 

class Panda extends Animal {
  ... 
  public bool compare(Animal) { ... }
}

From what I understand, problems with subtyping come up when something is done that could cause a loss of specificity. So what if I did this? :

Panda p = new Panda(); 
Animal a = new Animal
...
p.compare(a); 

When I look at this, it seems like panda could (and probably does) have some extra fields in it that a plain animal wouldn’t know about. Thus, even if all of their animal-specific data members are the same, a panda can have other stuff that differs. How would that make it okay to compare it to a plain animal? Would it just consider the animal-only stuff and ignore the rest?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:56:57+00:00Added an answer on June 14, 2026 at 9:56 pm

    In your example you don’t use any generic types. You have Panda extending Animal, and it’s an example of inheritance and leads to polymorphism which is more or less what you describe. Check the links.

    To get contravariance, you need to consider some generic type. I’ll use .NET type IComparer`1[T] as an example. With C# syntax (which I’ll use rather than Java), we indicate that IComparer is contravariant in T by writing in in the definition:

    public interface IComparer<in T>
    {
      ...
    }
    

    Suppose I have a method which returns an IComparer`1[Animal] (or IComaparer<Animal>), like:

    static IComparer<Animal> CreateAnimalComparer()
    {
      // code that returns something here
    }
    

    Now in C#, it’s legal to say:

    IComparer<Panda> myPandaComparer = CreateAnimalComparer();
    

    Now, this is because of contravariance. Note that the type IComparer<Animal> does not derive from (or “extend”) the type IComparer<Panda>. Instead, Panda derives from Animal, and this leads to the IComparer<Xxxx> being assignable to each other (in the opposite order, hence “contravariance” (not “covariance”)).

    The reason why it’s meaningful to declare a Comparer<> contravariant, is if you have a comparer that can compare two arbitrary animals, and return a signed number indicating which is greater, then that same comparer can also take in two pandas and compare those. For pandas are animals.

    So the relation

    any Panda is an Animal

    (from inheritance) leads to the relation

    any IComparer<Animal> is an IComparer<Panda>

    (by contravariance).

    For an example with covariance, the same relation

    any Panda is an Animal

    leads to

    any IEnumerable<Panda> is an IEnumerable<Animal>

    by covariance (IEnumerable<out T>).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just learned (the hard way) that Java Component s can only have one
I am new to programming in java and i have just learned how to
I have just started learning about socket programming and learned about winsock and achieved
I just learned the hard way that IntPtr.Zero cannot be compared to default(IntPtr). Can
I just learned about ThreadLocal this morning. I read that it should always be
I just learned of vim's gf command for navigating to files that are referenced
As my first programming language I learned Java, but since I changed to a
I have known PHP basics, functional programming for a long time before. But this
I've only just recently learned about Google's programming language, Go. I've been intrigued by
I have learned that the best way to learn a language is by finding

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.