Inheritance
C# Inheritance
Inheritance is the concept that is used for code reusability and changeability purpose. Here changeability means overriding the existed functionality or feature of the object or adding more functionality to the object. In c#, Inheritance is one of the primary concepts of object-oriented programming (OOP), and it is used to inherit the properties from one class (base) to another (child) class. The inheritance will enable us to create a new class by inheriting the properties from other classes to reuse, extend, and modify other class members, behavior based on our requirements.
We group the "inheritance concept" into two categories:
- Derived Class (child) - the class that inherits from another class
- Base Class (parent) - the class being inherited from
To inherit from a class, use the : symbol.
Types of inheritance
There are the following types of inheritance:
Single Level Inheritance
a single derived class inherits from a single base class.
Example
using System;
namespace MyApplication
{
public class School
{
public void DisplaySchool()
{
Console.WriteLine("My School Name Is XYZ");
}
}
public class Class1: School
{
public void DisplayClass1()
{
Console.WriteLine("This is Class1");
}
}
public class Program
{
public static void Main(string[] args)
{
Class1 c1 = new Class1();
c1.DisplayClass1();
c1.DisplaySchool();
}
}
}
OUTPUT
This is Class1
My School Name Is XYZ
Multi Level Inheritance
a derived class inherits from a base and then the same derived class acts as a base class for another class.
Example
using System;
namespace MyApplication
{
public class School
{
public School()
{
Console.WriteLine("Constructor called at run-time");
}
public void DisplaySchoolName()
{
Console.WriteLine("This is School XYZ");
}
}
public class Class1: School
{
public void DisplayClass1()
{
Console.WriteLine("This is Class1");
}
}
public class SectionA: Class1
{
public void DisplaySectionA()
{
Console.WriteLine("This is Section A of Class1");
}
}
public class Program
{
public static void Main(string[] args)
{
SectionA sa = new SectionA();
sa.DisplaySectionA();
sa.DisplayClass1();
sa.DisplaySchoolName();
}
}
}
OUTPUT
Constructor called at run-time
This is Section A of Class1
This is Class1
This is School XYZ
Hierarchical Inheritance
multiple derived classes inherit from a single base class.
Example
using System;
namespace MyApplication
{
public class School
{
public void DisplaySchoolName()
{
Console.WriteLine("This is School XYZ");
}
}
public class Class1: School
{
public void DisplayClass1()
{
Console.WriteLine("This is Class1");
}
}
public class Class2: School
{
public void DisplayClass2()
{
Console.WriteLine("This is Class2");
}
}
public class Program
{
public static void Main(string[] args)
{
Class1 c1 = new Class1();
Class2 c2 = new Class2();
c1.DisplayClass1();
c1.DisplaySchoolName(); // accessing School class
c2.DisplayClass2();
c2.DisplaySchoolName(); // accessing School class
}
}
}
OUTPUT
This is Class1
This is School XYZ
This is Class2
This is School XYZ