Give examples of functions which demonstrate covariance and contravariance in the cases of both overloading and overriding in Java? [closed]

Covariance: class Super { Object getSomething(){} } class Sub extends Super { String getSomething() {} } Sub#getSomething is covariant because it returns a subclass of the return type of Super#getSomething (but fullfills the contract of Super.getSomething()) Contravariance class Super{ void doSomething(String parameter) } class Sub extends Super{ void doSomething(Object parameter) } Sub#doSomething is contravariant because … Read more

What is a covariant return type?

Covariant return, means that when one overrides a method, the return type of the overriding method is allowed to be a subtype of the overridden method’s return type. To clarify this with an example, a common case is Object.clone() – which is declared to return a type of Object. You could override this in your … Read more

Why doesn’t the example compile, aka how does (co-, contra-, and in-) variance work?

Generically, a covariant type parameter is one which is allowed to vary down as the class is subtyped (alternatively, vary with subtyping, hence the “co-” prefix). More concretely: trait List[+A] List[Int] is a subtype of List[AnyVal] because Int is a subtype of AnyVal. This means that you may provide an instance of List[Int] when a … Read more

Co-variant array conversion from x to y may cause run-time exception

What it means is this Control[] controls = new LinkLabel[10]; // compile time legal controls[0] = new TextBox(); // compile time legal, runtime exception And in more general terms string[] array = new string[10]; object[] objs = array; // legal at compile time objs[0] = new Foo(); // again legal, with runtime exception In C#, … Read more

Why covariance and contravariance do not support value type

Basically, variance applies when the CLR can ensure that it doesn’t need to make any representational change to the values. References all look the same – so you can use an IEnumerable<string> as an IEnumerable<object> without any change in representation; the native code itself doesn’t need to know what you’re doing with the values at … Read more

Difference between Covariance & Contra-variance

The question is “what is the difference between covariance and contravariance?” Covariance and contravariance are properties of a mapping function that associates one member of a set with another. More specifically, a mapping can be covariant or contravariant with respect to a relation on that set. Consider the following two subsets of the set of … Read more

Covariance and contravariance real world example

// Contravariance interface IGobbler<in T> { void gobble(T t); } // Since a QuadrupedGobbler can gobble any four-footed // creature, it is OK to treat it as a donkey gobbler. IGobbler<Donkey> dg = new QuadrupedGobbler(); dg.gobble(MyDonkey()); // Covariance interface ISpewer<out T> { T spew(); } // A MouseSpewer obviously spews rodents (all mice are // … Read more

Why are arrays covariant but generics are invariant?

Via wikipedia: Early versions of Java and C# did not include generics (a.k.a. parametric polymorphism). In such a setting, making arrays invariant rules out useful polymorphic programs. For example, consider writing a function to shuffle an array, or a function that tests two arrays for equality using the Object.equals method on the elements. The implementation … Read more