Let’s understand what is Inheritance in OOP
What is Inheritance?
Inheritance enables a class or an object to 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 is called as a base class or a super class.
The class which marks itself as a derivative of the former is called as a derived class or a sub class.
Simply put — a child entity inheriting additional features other than its own, from a parent entity.
What are the different types of Inheritance?
There are four kinds of inheritance based on the ways in which a base and derived classes can relate to.
- Single-level
- Multi-level
- Multiple
- Hierarchical
- Hybrid
Single-level Inheritance
There is one base class, which is inherited by one derived class. All the features of the base class are available for use in the derived class.
public class ClassA
{
protected void DoSomething()
{
// something that Class A can only do
}
}
public class ClassB : ClassA
{
// DoSomething() is also available here
protected void DoSomethingElse()
{
// something that Class B can only do
}
}
Multi-level Inheritance
Multiple single-levels are chained together to form a series of inheritance. There is one super class, which is inherited by another, which is again inherited by some other and so on — forming a chain of inheriting classes.
public class ClassA
{
protected void DoSomething()
{
// something that Class A can only do
}
}
public class ClassB : ClassA
{
// DoSomething() is also available here
protected void DoSomethingElse()
{
// something that Class B can only do
}
}
public class ClassC : ClassB
{
// some functionality
// + ClassA accessibility
// + ClassB accessibility via ClassA
protected void DoNothing()
{
// something that ClassC can only do
}
}
Multiple Inheritance
A single class inherits from multiple base classes 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 a side effect called Diamond Dependency problem, which can impact compiler choice to link to the right implementation.
public class ClassA
{
protected void DoSomething()
{
// something that Class A can only do
}
}
public class ClassB
{
protected void DoSomethingElse()
{
// something that Class B can only do
}
}
// Doesn't work
// a Compile-time Error is thrown
public class ClassC : ClassA, ClassB
{
protected void DoNothing()
{
// something that ClassC can only do
}
}
Hierarchical Inheritance
In this type, there is a single base class which is extended by multiple child classes forming a tree sort of dependency chain.
public class ClassA
{
protected void DoSomething()
{
// something that Class A can only do
}
}
public class ClassB : ClassA
{
protected void DoSomethingElse()
{
// something that Class B can only do
}
}
public class ClassC : ClassA
{
// some functionality
// + ClassA accessibility
protected void DoNothing()
{
// something that ClassC can only do
}
}
Hybrid Inheritance
Hybrid Inheritance is when you combine one or more types mentioned above according to the need.
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.
A derived class object can be assigned to a base class reference, which is not generally possible.
ClassA a = new ClassA();
a.DoSomething();
ClassA b = new ClassB();
// ClassA function called via inheritance
b.DoSomething();
// ClassB function called via reference
b.DoSomethingElse();
What is Method Overriding?
It is a scenario in which a base class and a derived class have the same method signature but different implementations.
The compiler needs to decide on which implementation to be called (parent or child) based on how the methods are scoped and referred.
The child class method overrides the parent class method. In C# we can vary the calling via override, new and virtual keywords.
Read the full article here — https://referbruv.com/blog/object-oriented-programming-concepts-inheritance/