Static Inheritance
Inheritance is a core feature of object oriented languages that has been used to simulate real world by modeling closely related objects and to build reusable code. The inheritance relationship is defined statically in class specifications and it comes in various flavors such as:
Single Inheritance
It allows a class to be extended by just one other class.
Multiple Inheritance
It allows a class to be derived from multiple classes and historically has been difficult to maintain and has been source of diamond inheritance in C++, though other languages use order such as Method Resolution Order (MRO) in Python to avoid those issues.
Interfaces
The interfaces are used in C# and Java to define methods without implementation and a class can implement multiple interfaces without the downsides of multiple inheritance.
Mixins
The mixins are available in Ruby and D, that use mixins for code reuse. The mixins are similar to interfaces with implementations except they aggregate methods and attributes at runtime.
Traits
The traits are available in Squeak and Scala and are conceptually similar to Mixins except traits do not allow attributes.
Dynamic Inheritance
As opposed to static inheritance, dynamic inheritance can be added at runtime using Object Extension Pattern, which I first learned in Erich Gamma, et al’s Gof patterns. In late 90s, I used Voyager ORB for building distributed systems, which used this pattern. Following example shows how this pattern can be used:
Let’s define a marker interface Extension in Java such as:
1 package ext; 2 3 public interface Extension { 4 5 } 6 7