Inheritance In C#
What is Inheritance
?
Inheritance is one of the core concepts of the OOPS
.The definition of Inheritance is one class deriving the property of another class is known as inheritance.
The class from which we are going to inherit the properties then that class is known as
superclass
orbase class
orparent class
.The class which is inheriting the property of the class is known as the
subclass
orderived class
orchild class
.
class Subclass : SuperClass
{
//methods and fields
}
Note: In
C#
we use:
to inherit other classes.Why do we need
inheritance
?
Using inheritance we can reuse the code instead of rewriting the code again and again.
Using inheritance the number of lines of code can be reduced.
Types of inheritance :
These are the types of inheritance.
- Single Inheritance.
- Multilevel Inheritance.
- Multiple Inheritance.
- Hierarchical Inheritance.
- Hybrid Inheritance.
1. Single Inheritance :
- One class inheriting the properties of another class is called as
Single inheritance
.
class A
{
public void display()
{
Console.WriteLine("display() of Superclass");
}
}
class B : A
{
public void view()
{
Console.WriteLine("view() of ChildClass");
}
}
class Program
{
public static void Main()
{
B b = new B();
//Using this the property of the superclass can also be called
b.view(); //Sub class method
b.display(); //Super class method
}
}
/*
Output :
view() of ChildClass
display() of Superclass
*/
2. Multilevel Inheritance :
In this type of inheritance subclass
inheritance, the properties of the superclass
and this superclass
inherit the properties of another superclass
is known as the Multilevel Inheritance
.
class A
{
public void display()
{
Console.WriteLine("display() of class of A");
}
}
class B : A
{
public void view()
{
Console.WriteLine("view() of class of B");
}
}
class C : B
{
public void run()
{
Console.WriteLine("run() of class of C");
}
}
class Program
{
public static void Main()
{
C c = new C();
c.run();
c.view();
c.display();
}
}
*/
Output :
run() of class of C
view() of class of B
display() of class of A
*/
Note: We can access the properties of all the classes using the Object of class
C
becauseC
is inheriting the classB
and classB
is inheriting the classA
.
3. Multiple Inheritance :
In this type of inheritance one, subclass
inheriting the properties of the multiple classes is known as Multiple Inheritance
.
Note: The
multiple inheritance is not supported in C#
.
4. Hierarchical Inheritance :
In this type of inheritance from one superclass
, multiple subclasses
inheritances the properties this creates the hierarchical structure so, this is called as the Hierarchical Inheritance
.
class Car
{
public void drive()
{
Console.WriteLine("drive() of the Car class");
}
}
class Tesla : Car
{
public void electric()
{
Console.WriteLine("electric() of Tesla");
}
}
class RollsRoyce : Car
{
public void luxury()
{
Console.WriteLine("luxury() of Rolls-Royce");
}
}
class Program
{
public static void Main()
{
Tesla tesla = new Tesla();
tesla.drive();
tesla.electric();
RollsRoyce rolls = new RollsRoyce();
rolls.drive();
rolls.luxury();
}
}
/*
Output :
drive() of the Car class
electric() of Tesla
drive() of the Car class
luxury() of Rolls-Royce
*/
Note: In both the
subclasses
the basic property that isdrive()
is inherited along with their own methods.
5. Hybrid Inheritance :
Hybrid Inheritance is the combination of the other type of inheritance.