2. Content
What is Inheritance
Important terminology
How to perform
inheritance
Types of Inheritance
Advantages of
inheritance
Conclusion
3. Inheritance
In C# , inheritance allows us to create a new class from an
existing class. It is a key feature of Object-Oriented
Programming (OOP). The derived class or The child class will
inherit fields and methods all the public and protected
properties of the base class or also known as parent class In
addition, it can have its own properties and methods. This helps
with the code reusability in C#.
4. Important terminology:
Super Class: Sub Class:
The class that inherits the
other class is known as
subclass
Reusability:
• Inheritance supports the concept
of “reusability”, I.e. when we
want to create a new class and
there is already a class that
includes some of the code that
we want, we can derive our new
class from the existing class.
Is a Relationship:
We use inheritance only if
there is an is-a
relationship between two
classes.
The class whose features
are inherited is known as
super class
7. ● In single inheritance, subclasses
inherit the features of one
superclass. In image below, the
class A serves as a base class for
the derived class B.
Single Inheritance
8. class Fruit
{
public void Display()
{
Console.WriteLine("Fruit");
}
}
//Our derived class is Bananna and is declared
below −
class Banana : Fruit
{
public void DisplayOne()
{
Console.WriteLine("Banana is Fruit ");
}
}
Single Inheritance
9. In multilevel
inheritance, a derived
class inherits from a
base and then the
same derived class
acts as a base class
for another class.
Multilevel inheritance
10. Example About Multilevel
Inheritance:
In the preceding program, each
class is derived from one class
that is derived from another class
hence this type of inheritance is
called Multilevel Inheritance.
11. class Fruit
{
public void Display()
{
Console.WriteLine("Fruit");
}
}
//Our derived class is Bananna and is declared
below −
class Banana : Fruit
{
public void DisplayOne()
{
Console.WriteLine("Banana is Fruit ");
}
}
Multilevel inheritance
13. In the preceding program one base class is
derived in many classes hence it is a called
a Hierarchical Inheritance
Example About Hierarchal
Inheritance
14. class Fruit
{
public void Display()
{
Console.WriteLine("Fruit");
}
}
//Our derived class is Bananna and is declared
below −
class Banana : Fruit
{
public void DisplayOne()
{
Console.WriteLine("Banana is Fruit ");
}
}
Multilevel inheritance
15. Advantages of inheritance
It helps in using the same code again means code reusability.
It reduces code redundancy.
It helps in reading the code more comfortably.
It also reduces the size of the source code and file.
It helps in providing the extensibility to code.
Private members are not accessed in derived class when base class
members are inherited by the derived class.
The code is easy to manage as it divided into classes of the base
class and child class.