Inheritance in an Object Oriented paradigm is similar to the real-world understanding. Inheritance is an occurrence when a class or an object can access all the data and functionalities from another class by making itself a "derivative" of the other class. The class which is the original source of information and functionality can be called as a "base class" or a "super class" and the class which marks itself as a derivative of the former class can be called as a "derived class" or a "sub class".
Inheritance is all about a "child" entity "inheriting" additional features other than its own, from a "parent" entity
There are four kinds of inheritance based on the way in which a base and derived classes are linked.
Single Inheritance:
There is a single base class, which is inherited by a single derived class. The simplest of all kinds, where all the features of the base class are available for use in the derived class.
public class ClassA
{
// some functionality
}
public class ClassB : ClassA
{
// some functionality + ClassA accessibility
}
Multilevel Inheritance:
Multiple single inheritance levels are chained together to form a series of inheritance. There is a super class Class A, which is inherited by the class Class B, which is again inherited by another class Class C and so on, forming a chain of inheriting classes.
public class ClassA
{
// some functionality
}
public class ClassB : ClassA
{
// some functionality + ClassA accessibility
}
public class ClassC : ClassB
{
// some functionality
// + ClassA accessibility
// + ClassB accessibility via ClassA
}
Multiple Inheritance:
A single class ClassC inherits from multiple base classes (say ClassA, ClassB ..) together, trying to obtain functionalities from all of them together. This is not directly allowed in most of the Object Oriented programming languages, because of its side effect which can impact the compilation.
/* Not Allowed */
public class ClassA
{
// some functionality
}
public class ClassB
{
// some functionality
}
public class ClassC : ClassA, ClassB
{
// some functionality
// + ClassA accessibility
// + ClassB accessibility
}
Instead multiple inheritance can be achieved by extending from a single base class and multiple interfaces.
/* Allowed */
public class ClassA
{
// some functionality
}
public interface InterfaceB
{
// some functionality
}
public class ClassC : ClassA, InterfaceB
{
// some functionality + ClassA accessibility + InterfaceB implementation
}
A class can extend only one base class but can implement mulitple interfaces.
Hierarchical Inheritance:
In this type, there is a single base class ClassA which is extended by multiple child classes ClassB, ClassC, .. forming a tree sort of schema.
public class ClassA
{
// some functionality
}
public class ClassB : Class A
{
// some functionality + ClassA accessibility
}
public class ClassC : ClassA
{
// some functionality + ClassA accessibility
}
Hybrid Inheritance:
Hybrid Inheritance is a sort of combination of any of the above types of inheritance together.
Accessibility levels in Inheritance:
When discussing Encapsulation, we were looking at how access modifiers can alter access to an element of a class from outside access. In that scope, there was also the inherited scope along with local and referential; which describes how an element can be accessed from any of the three possible means of invocation.
"Public and Protected members of a base class can be directly accessed within a derived class without any issues, whereas the private members are private to the base class and cannot be accessed by the derived class."
The "is-a" relationship:
When two classes are related to one another by means of inheritance, there can be a slight difference in how these can be instantiated. For example, assume there is a base class ClassA and a derived class ClassB. The class ClassA can be instantiated as:
ClassA a = new ClassA();
and the class ClassB as:
ClassB b = new ClassB();
We can also change this instantiation as,
ClassA a = new ClassB();
In the above statement, we’re creating an instance of type ClassB (the right hand side) and assigning it to a type ClassA (the left side). This is completely valid syntactically in an object oriented programming because a subclass "is-a" base class.
Final Words:
- Inheritance is about extending a class functionality into its successor class by a link
- There are several kinds of inheritance such as Single, Multilevel, Multiple, Hierarchial and Hybrid
- A class cannot extend from multiple base classes, but can implement multiple interfaces
- Inheritance can provide access to the base class elements in the derived class but is limited to public and protected only
- When two classes are linked together as a base class and a derived class, a derived class "is-a" base class