Inheritance | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

Inheritance

Date- Dec 09,2023

1376

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

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
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 | 1190
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

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts