Topic: Contra vs. Co


Author: ivin@corvus.EECS.Berkeley.EDU (Ilya Vinarsky)
Date: 26 Feb 1995 05:09:21 GMT
Raw View
I've heard these terms used, but I don't now what they mean: contravariant
and covariant type.  What do they mean?

-------------------------------------------------------------------------------
Ilya Vinarsky When smashing monuments, save the pedestals -
  They always come in handy.
      Stanislaw Jerzy Lec.




Author: arnoud@ijssel.xs4all.nl (Arnoud Martens)
Date: Sun, 26 Feb 1995 19:33:33 GMT
Raw View
In article <3ip2e1$c65@agate.berkeley.edu>,
Ilya Vinarsky <ivin@corvus.EECS.Berkeley.EDU> wrote:
>I've heard these terms used, but I don't now what they mean: contravariant
>and covariant type.  What do they mean?

These terms come from typing theory and apply equally well to
inheritance:

Covariant means restriction. Generally covariance must be obeyed in
the attributes when inheriting. That means the set of values an
attribute of a Derived class may have is equal or less to it's base
class. this is the same of saying the set of instances of the derived
class is a subset of the set of all instances of the Base Class.

Contravariance is the opposite: as you descend in your inheritance
tree the set of values may be expanded.

Oversimplified: in OOP covariance must apply to the return types of
methods. Contravariance applies to the parameters passed to the
methods. This means you may substitute a method of a derived class for
a method of a base class (and thereby substitute an instance of a
derived class for one of a base class). This enables polymorphism is
one of the key elements to Object Oriented programming.

Think your question is more appropriate for comp.object so I have
redirected follow-ups to that group.

--
Name: Arnoud Martens, Utrecht, the Netherlands,  tel: +31-30-732679
E-mail: arnoudm@ijssel.xs4all.nl WWW: http://www.xs4all.nl/~arnoudm