Is-a

In knowledge representation and ontology components, including for object-oriented programming and design, is-a (also written as is_a or is a) is a subsumptive relationship between abstractions (e.g., types, classes), wherein 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.

For example, a cat 'is a' animal, but not vice versa. All cats are animals, but not all animals are cats. Behaviour that is relevant to all animals is defined on an animal class, whereas behaviour that is relevant only for cats is defined in a cat class. By defining the cat class as 'extending' the animal class, all cats 'inherit' the behaviour defined for animals, without the need to explicitly code that behaviour for cats.

Related concepts
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:


 * hyperonym–hyponym (supertype/superclass–subtype/subclass) relations between types (classes) defining a taxonomic hierarchy, where
 * for a subsumption relation: a hyponym (subtype, subclass) has a type-of (is-a) relationship with its hyperonym (supertype, superclass);
 * holonym–meronym (whole/entity/container–part/constituent/member) relations between types (classes) defining a possessive hierarchy, where
 * for an aggregation (i.e. without ownership) relation:
 * a holonym (whole) has a has-a relationship with its meronym (part),
 * for a composition (i.e. with ownership) relation:
 * a meronym (constituent) has a part-of relationship with its holonym (entity),
 * for a containment relation:
 * a meronym (member) has a member-of relationship with its holonym (container);
 * concept–object (type–token) relations between types (classes) and objects (instances), where
 * a token (object) has an instance-of relationship with its type (class).

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).

Python
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.

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 implements List, and List extends Collection. So ArrayList is a subtype of List, which is a subtype of Collection. 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:

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,". 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.