C# - SPLessons

C# Class Inheritance

Home > Lesson > Chapter 11
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C# Class Inheritance

C# Class Inheritance

shape Description

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.

shape Advantages

    1. Code is re-used.
    2. Method over-riding.
    3. Reduces execution time.

Access modifiers for using inheritance

shape 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.

shape Conceptual figure

shape Program Description

Overriding

shape 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.

Types of Inheritance

shape Conceptual figure

Single Inheritance

shape Description

Single Child class inherits the properties from single Parent class.

Multi-level Inheritance

shape Description

A child class inherits from parent class which in turn inherits from another parent class.

Multiple Inheritance

shape Description

A single child class inherits from many parent classes.

Hierarchical Inheritance

shape Description

Multiple child classes derives from single parent class.

Hybrid Inheritance

shape Description

Hybrid inheritance is formed by the combination of multi-level and hierarchical inheritances.

shape 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: