Content deleted Content added
→Inheritance in object-oriented languages: Added helpful description, to avoid having to look up UML. |
→Inheritance in object-oriented languages: added more helpful description to align with the terminology used in the rest of the text |
||
Line 110:
== Inheritance in object-oriented languages ==
When a subclass [[Method overriding|overrides]] a method in a superclass, the compiler must check that the overriding method has the right type. While some languages require that the type exactly matches the type in the superclass (invariance), it is also type safe to allow the overriding method to have a "better" type. By the usual subtyping rule for function types, this means that the overriding method should return a more specific type (return type covariance), and accept a more general argument (parameter type contravariance). In [[Unified Modeling Language|UML]] notation, the possibilities are as follows (where Class B is the subclass that extends Class A which is the superclass):
<gallery perrow="5" heights="190" caption="Variance and method overriding: overview">
|