Is-a
In knowledge representation, object-oriented programming and design, is-a or is_a or is a (subsumption) is a relationship where one class A is a subclass of another class B (and so B is a superclass of A). In other words, Type A is a subtype of type B when A’s specification implies B’s specification. That is, any object (or class) that satisfies A’s specification also satisfies B’s specification, because B’s specification is weaker.[1]
The is-a relationship is contrasted with the has-a relationship, which constitutes the holonym-meronym hierarchy. It may also be contrasted with the instance-of relation: see "Type-token distinction" and "Type-token relations".[2] When designing a model (e.g., a computer program) of the real-world relationship between an object and its subordinate, a common error is confusing the relations has-a and is-a.
Examples of Subtyping
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.
C++
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).
class A { public: void DoSomethingALike() const {} }; class B : public A { public: void DoSomethingBLike() const {} }; void UseAnA(A const& some_A) { some_A.DoSomethingALike(); } void SomeFunc() { B b; UseAnA(b); // b can be substituted for an A. }
Python
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.[4]
>>> a = 0 >>> type(a) <type 'int'> >>> type(type(a)) <type 'type'> >>> type(type(type(a))) <type 'type'> >>> type(type(type(type(a)))) <type 'type'>
Java
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 we define an interface, PayloadList, that associates an optional value of generic type P with each element. Its declaration might look like:
interface PayloadList<E,P> extends List<E> { void setPayload(int index, P val); ... }
The following parameterizations of PayloadList are subtypes of List<String>:
PayloadList<String,String> PayloadList<String,Integer> PayloadList<String,Exception>
Liskov Substitution Principle
- "FUNCTIONS THAT USE POINTERS OR REFERENCES TO BASE CLASSES MUST BE ABLE TO USE OBJECTS OF DERIVED CLASSES WITHOUT KNOWING IT',"
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,".[5] Following example shows a violation of LSP.
void DrawShape(const Shape& s) { if (typeid(s) == typeid(Square)) DrawSquare(static_cast<Square&>(s)); else if (typeid(s) == typeid(Circle)) DrawCircle(static_cast<Circle&>(s)); } }
Obviously DrawShape function is badly formatted. It has to know about every derivative classes of Shape class. Also it should be changed whenever new subclass of Shape are created. In Object Oriented Design, many view the structure of this as anathema.
Here is a more subtle example of violation of LSP
class Rectangle { public: void SetWidth(double w) {itsWidth=w;} void SetHeight(double h) {itsHeight=h;} double GetHeight() const {return itsHeight;} double GetWidth() const {return itsWidth;} private: double itsWidth; double itsHeight; };
This works well but when it comes to Square class, which inherits Rectangle class, it violates LSP even though the is-a relationship holds between Rectangle and Square. Because square is rectangular. The following example overrides two functions, Setwidth and SetHeight, to fix the problem. But fixing the code implies that the design is faulty.
public class Square : Rectangle { public: virtual void SetWidth(double w); virtual void SetHeight(double h); }; void Square::SetWidth(double w) { Rectangle::SetWidth(w); Rectangle::SetHeight(w); } void Square::SetHeight(double h) { Rectangle::SetHeight(h); Rectangle::SetWidth(h); }
The following example, function g just works for Rectangle class but not for Square, and so the Open-Closed principle has been violated.
void g(Rectangle& r) { r.SetWidth(5); r.SetHeight(4); assert(r.GetWidth() * r.GetHeight()) == 20); }
See also
- Has-a
- Hyponym
- Inheritance (object-oriented programming)
- Liskov substitution principle (in object-oriented programming)
- Subsumption
- Subtype
References
- Ronald J. Brachman; What IS-A is and isn't. An Analysis of Taxonomic Links in Semantic Networks. IEEE Computer, 16 (10); October 1983
- Jean-Luc Hainaut, Jean-Marc Hick, Vincent Englebert, Jean Henrard, Didier Roland: Understanding Implementations of IS-A Relations. ER 1996: 42-57
- ↑ "Subtypes and Subclasses". MIT OCW. Retrieved 2 October 2012.
- ↑ https://en.wikipedia.org/wiki/Type%E2%80%93token_relations
- ↑ 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.
- ↑ Guido van Rossum. "Subtyping Built-in Types". Retrieved 2 October 2012.
- ↑ Liskov, Barbara (May 1988). Data Abstraction and Hierarchy. SIGPLAN Notices.
- ↑ "The Liskov Substitution Principle". Robert C. Martin, 1996. Retrieved 2 October 2012.