Is-a


In knowledge representation, object-oriented programming and design, is-a is a relationship between abstractions, wherein one class A is a subclass of another class B.
In other words, type A is a subtype of type B when A's specification implies B's specification. That is, any object that satisfies A's specification also satisfies B's specification, because B's specification is weaker.
The is-a relationship is to be contrasted with the has-a relationship between types ; confusing the relations has-a and is-a is a common error when designing a model 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 and types : see "type-token distinction" and "type-token relations."
To summarize the relations, there are:
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.
class A
class B : public A
void UseAnA
void SomeFunc

Python

The following python code establishes an explicit inheritance relationship between classes and, where is both a subclass and a subtype of, and can be used as an wherever a is required.

class A:
def do_something_a_like:
pass
class B:
def do_something_b_like:
pass
def use_an_a:
some_a.do_something_a_like
def some_func:
b = B
use_an_a # b can be substituted for an A.

The following example, is a "regular" type, and is a metatype. While as distributed all types have the same metatype, this is not a requirement. The type of classic classes, known as, can also be considered a distinct metatype.

>>> a = 0
>>> type

>>> type

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

interface PayloadList extends List

The following parameterizations of PayloadList are subtypes of List:

PayloadList
PayloadList
PayloadList

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.
void DrawShape

Obviously, the 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

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
void Square::SetWidth
void Square::SetHeight

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