Inheritance is a concept of OOP programming. It enables a class to inherit another class property, hence member functions or methods and variables of a class can be accessed by another class if it is inherited from it. Inheritance makes a connection of a class to another class, thus making it accessible.
There are some types of Inheritance,
1. Single Level Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multi-Level Inheritance
5. Hybrid Inheritance
1. Single Level Inheritance : In single level inheritance a class inherits the properties of one single class.
using System;
namespace Learn2Code{
class First{
public void message1()
{
Console.WriteLine("Hi from first class.");
}
}
// Single Inheritance
class Second: first{
public void message2{
Console.WriteLine("Hi from second class.");
}
}
class Program{
static void Main(string[] args)
{
Second s = new Second();
// Both methods are called with single object of Class Second due to Single inheritance.
s.message1();
s.message2();
}
}
}
2, Multiple Inheritance : In Multiple Inheritance there are multiple base classes and a single child or derived class.
using System;
namespace Learn2Code{
class First{
public void message1()
{
Console.WriteLine("Hi from first class.");
}
}
class Second{
public void message2{
Console.WriteLine("Hi from second class.");
}
}
class Three: First, second{
public void message3
{
Console.WriteLine("Hi from class Three.");
}
}
class Program{
static void Main(string[] args)
{
Three = new Three();
// Both methods are called with single object of Class Three due to Multiple inheritance.
s.message1();
s.message2();
s.message3();
}
}
}
3. Hierarchical Inheritance : In Hierarchical Inheritance there is a single base class and multiple derived classes.
using System;
namespace Learn2Code{
class First{
public void message1()
{
Console.WriteLine("Hi from first class.");
}
}
class Second: First{
public void message2{
Console.WriteLine("Hi from second class.");
}
}
class Three: First {
public void message3
{
Console.WriteLine("Hi from class Three.");
}
}
class Program{
static void Main(string[] args)
{
Second s = new Second();
// Both methods are called with single object of Class Second due to Hierarichcal inheritance.
s.message1();
s.message2();
Three t = new Three();
t.message1();
t.message3();
}
}
}
4. Multi-Level Inheritance : In Multi-Level Inheritance there is one base class which has one derived class and that derived class can have another derived class hence making it a base class. This can go to several levels.
using System;
namespace Learn2Code{
class First{
public void message1()
{
Console.WriteLine("Hi from first class.");
}
}
class Second: First{
public void message2{
Console.WriteLine("Hi from second class.");
}
}
class Three: Second {
public void message3
{
Console.WriteLine("Hi from class Three.");
}
}
class Program{
static void Main(string[] args)
{
Second s = new Second();
// Both methods are called with single object of Class Second due to Multi-level Inheritance inheritance.
s.message1();
s.message2();
Three t = new Three();
t.message1();
t.message2();
t.message3();
}
}
}
5. Hybrid Inheritance : In Hybrid Inheritance there can be two or more types of inheritance between classes.