In object-oriented programming, inheritance refers to the ability of an object to take on one or more characteristics from other classes of objects. The characteristics inherited are usually instance variables or member functions. An object that inherits these characteristics is known as a subclass. The object it inherits them from is known as a superclass. The details of how inheritance is implemented vary between languages, but the first language to implement it was Simula in 1967.
What is inheritance used for?
The purpose of inheritance is to consolidate and re-use code. For example, if the objects “car,” “truck,” and “motorcycle” are subclasses of vehicle, code applying to all of them can be consolidated into a vehicle superclass. The subclasses inherit this code and future changes made to it, automatically.
Five types of inheritance
- Single inheritance - Subclasses inherit characteristics from a single superclass.
- Multiple inheritance - A subclass may have more than one superclass and inherit characteristics from all of them.
- Multilevel inheritance - A subclass may have its own subclasses. In other words, a subclass of a superclass can itself be a superclass to other subclasses.
- Hierarchical inheritance - A base class acts as the parent superclass to multiple levels of subclasses.
- Hybrid inheritance - A combination of one or more of the other inheritance types.
Instance, Object-oriented, Programming terms