mike-obrien.net Curriculum Vitae Blog Labs
Sunday, October 15, 2006

In OOP, type variance refers to the difference between a class and its descendant or ascendant. For example lets say we have three classes; Vehicle, Spacecraft and Shuttle. Shuttle inherits from Spacecraft which inherits from Vehicle. They all vary by type. Vehicle is the contravariant of Spacecraft since Vehicle is Spacecraft’s ancestor whereas Shuttle is the covariant of Spacecraft since Shuttle is a descendant of Spacecraft. Invariance simply refers to a lack of variance. For example a parameter or return type that must be invariant cannot respectively accept or return an ancestor or descendant of the type specified, they can only accept or return the specified type.

Sunday, October 15, 2006 6:33:35 AM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  |  Related posts:
MIT Open Courseware, Part II
MIT OpenCourseWare

Comments are closed.