Inheritance (object-oriented programming)

The relationships of objects or classes through inheritance give rise to a directed acyclic graph.

Class-modifying inheritance patterns can be pre-defined according to simple network interface parameters such that inter-language compatibility is preserved.

[8] Influenced by this, in 1967 Ole-Johan Dahl and Kristen Nygaard presented a design that allowed specifying objects that belonged to different classes but had common properties.

For example, in a summary of C++ in his book on Objective C, Brad Cox actually claimed that adding multiple inheritance to C++ was impossible.

Since I had considered multiple inheritance as early as 1982 and found a simple and efficient implementation technique in 1984, I couldn't resist the challenge.

The general form of defining a derived class is:[13] Some languages also support the inheritance of other constructs.

The superclass establishes a common interface and foundational functionality, which specialized subclasses can inherit, modify, and supplement.

Such non-subclassable classes restrict reusability, particularly when developers only have access to precompiled binaries and not source code.

Because the exact type of the object being referenced is known before execution, early binding (also called static dispatch) can be used instead of late binding (also called dynamic dispatch), which requires one or more virtual method table lookups depending on whether multiple inheritance or only single inheritance are supported in the programming language that is being used.

An invocation of a non-virtual method will always be statically dispatched (i.e. the address of the function call is determined at compile-time).

The following table shows which variables and functions get inherited dependent on the visibility given when deriving the class, using the terminology established by C++.

Many object-oriented programming languages permit a class or object to replace the implementation of an aspect—typically a behavior—that it has inherited.

In the following Python example, subclasses SquareSumComputer and CubeSumComputer override the transform() method of the base class SumComputer.

In most quarters, class inheritance for the sole purpose of code reuse has fallen out of favor.

An alternative technique, explicit delegation, requires more programming effort, but avoids the substitutability issue.

[16] Another frequent use of inheritance is to guarantee that classes maintain a certain common interface; that is, they implement the same methods.

Often, there is no interface change between the supertype and subtype- the child implements the behavior described instead of its parent class.

For example, 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).

For example, the decorator pattern (as mentioned above) has been proposed to overcome the static nature of inheritance between classes.

As a more fundamental solution to the same problem, role-oriented programming introduces a distinct relationship, played-by, combining properties of inheritance and composition into a new concept.

[citation needed] According to Allen Holub, the main problem with implementation inheritance is that it introduces unnecessary coupling in the form of the "fragile base class problem":[6] modifications to the base class implementation can cause inadvertent behavioral changes in subclasses.

Single inheritance
Multiple inheritance
Multilevel inheritance
Illustration of method overriding