The is-a relationship is to be contrasted with the has-a (has_a or has a) relationship between types (classes); confusing the relations has-a and is-a is a common error when designing a model (e.g., a computer program) of the real-world relationship between an object and its subordinate. The is-a relationship may also be contrasted with the instance-of relationship between objects (instances) and types (classes): see Type–token distinction.
To summarize the relations, there are:
Subtyping enables a given type to be substituted for another type or abstraction. Subtyping is said to establish an is-a relationship between the subtype and some existing abstraction, either implicitly or explicitly, depending on language support. The relationship can be expressed explicitly via inheritance in languages that support inheritance as a subtyping mechanism.
The following C++ code establishes an explicit inheritance relationship between classes B and A, where B is both a subclass and a subtype of A, and can be used as an A wherever a B is specified (via a reference, a pointer or the object itself).
4
The following python code establishes an explicit inheritance relationship between classes B and A, where B is both a subclass and a subtype of A, and can be used as an A wherever a B is required.
The following example, type(a) is a "regular" type, and type(type(a)) is a metatype. While as distributed all types have the same metatype (PyType_Type, which is also its own metatype), this is not a requirement. The type of classic classes, known as types.ClassType, can also be considered a distinct metatype.5
In Java, is-a relation between the type parameters of one class or interface and the type parameters of another are determined by the extends and implements clauses.
Using the Collections classes, ArrayList<E> implements List<E>, and List<E> extends Collection<E>. So ArrayList<String> is a subtype of List<String>, which is a subtype of Collection<String>. The subtyping relationship is preserved between the types automatically. When defining an interface, PayloadList, that associates an optional value of generic type P with each element, its declaration might look like:
The following parameterizations of PayloadList are subtypes of List<String>:
Main article: Liskov substitution principle
Liskov substitution principle explains a property, "If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T, the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T,".6 Following example shows a violation of LSP.
Here is perhaps an example of violation of LSP:
From a programing point of view, the Square class may be implemented by inheriting from the Rectangle class.
However, this violates LSP even though the is-a relationship holds between Rectangle and Square
Consider the following example, where function g does not work if a Square is passed in, and so the open-closed principle might be considered to have been violated.
Conversely, if one considers that the type of a shape should only be a constraint on the relationship of its dimensions, then it is the assumption in g() that SetHeight will change height, and area, but not width that is invalid, not just for true squares, but even potentially for other rectangles that might be coded so as to preserve area or aspect ratio when height changes.
7
See Liskov substitution principle. /wiki/Liskov_substitution_principle ↩
"Subtypes and Subclasses" (PDF). MIT OCW. Retrieved 2 October 2012. http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-170-laboratory-in-software-engineering-fall-2005/lecture-notes/lec14.pdf ↩
See also Containment (computer programming). /wiki/Containment_(computer_programming) ↩
Mitchell, John (2002). "10 "Concepts in object-oriented languages"". Concepts in programming language. Cambridge, UK: Cambridge University Press. p. 287. ISBN 0-521-78098-5. 0-521-78098-5 ↩
Guido van Rossum. "Subtyping Built-in Types". Retrieved 2 October 2012. https://www.python.org/dev/peps/pep-0253/ ↩
Liskov, Barbara (May 1988). Data Abstraction and Hierarchy (PDF). SIGPLAN Notices. Archived from the original on Jun 21, 2020. https://web.archive.org/web/20200621040810/https://klevas.mif.vu.lt/~plukas/resources/OODPrinciples/Liskov1987.pdf ↩
"The Liskov Substitution Principle" (PDF). Robert C. Martin, 1996. Archived from the original (PDF) on 5 September 2015. Retrieved 2 October 2012. https://web.archive.org/web/20150905081111/http://www.objectmentor.com/resources/articles/lsp.pdf ↩