C# Class Inheritance is one of the important feature of Object oriented Programming (OOP) and considered as second strong pillar of OOP’s .Inheritance is the creation of new class acquiring the properties and characteristics of already existing class.
The class which gives its properties to newly generated class is called as Super class (or) Parent class (or) Base class.
The class which inherits or takes the properties from the existing class is called as Sub class (or) Child class (or) Derived class.The child class can even have its own characteristics.
Advantages
Code is re-used.
Method over-riding.
Reduces execution time.
Access modifiers for using inheritance
Description
Access modifiers decides whether the super class members are available for inheritance or not by sub class.
public:
public members are accessible in its own class(Base class).
public members are accessible from derived class.
public members are accessible outside the derived class.
private
private members are accessible in its own class(Base class).
private members are not accessible from derived class.
private members are not accessible outside the derived class.
protected
protected members are accessible in its own class(Base class).
protected members are accessible from derived class.
protected members are not accessible outside the derived class.
Conceptual
figure
Program
Description
Overriding
Description
Assume that both super class and sub class have same name and same number of arguments. Now, if object is created in sub class and member functions are given access, then the sub class member functions are taken into consideration not the super class.This process of accessing sub class member functions is called as overriding, as sub class overrides super class member functions.
Note
In C#, one cannot override a member of a class unless it's marked as virtual. If you want to access it then , you can still access the inherited method, even when you override it, using the base keyword.
Types of Inheritance
Conceptual
figure
Single Inheritance
Description
Single Child class inherits the properties from single Parent class.
Multi-level Inheritance
Description
A child class inherits from parent class which in turn inherits from another parent class.
Multiple Inheritance
Description
A single child class inherits from many parent classes.
Hierarchical Inheritance
Description
Multiple child classes derives from single parent class.
Hybrid Inheritance
Description
Hybrid inheritance is formed by the combination of multi-level and hierarchical inheritances.
Example
In the below example you can see the basic example.
[csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SPLessons
{
class Program
{
static void Main(string[] args)
{
Animal a=new Cat();
a.Display();
Console.ReadLine();
}
public class Animal
{
public virtual void Display()
{
Console.WriteLine("From Base Class");
}
}
public class Cat:Animal
{
public override void Display()
{
Console.WriteLine("Derived Class");
}
}
}
}
[/csharp]
Output: