Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Inheritance

Date- Dec 09,2023

3205

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

S
Shubham Saini
Programming author at Code2Night โ€” sharing tutorials on ASP.NET, C#, and more.
View all posts โ†’

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ยท  Terms
Translate Page