C# - Inheritance of Abstract Classes Example

In this example, we will learn how to implement inheritance of abstract classes using C# program?
Submitted by Nidhi, on October 26, 2020 [Last updated : March 21, 2023]

Here, we will implement the inheritance of abstract classes; here we will inherit one abstract class into another abstract class.

C# program to implement inheritance of abstract classes

The source code to demonstrate the inheritance of abstract classes is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to inherit an abstract class 
//in another abstract class.

using System;

abstract class Abs1
    //Method Declaration
    public abstract void Method1();

abstract class Abs2 : Abs1
    //Method Declaration
    public abstract void Method2();

class Sample : Abs2
    //Method definition
    public override void Abs1.Method1()
        Console.WriteLine("Method1() called");

    public override void Abs2.Method2()
        Console.WriteLine("Method2() called");

class Program
    public static void Main(String[] args)
        Abs1 M1;
        Abs2 M2;

        M1 = new Sample();
        M2 = new Sample();



Method1() called
Method2() called
Press any key to continue . . .


Here, we created two abstract classes Abs1 and Abs2. Here, we inherited the abstract class Abs1 into Abs2. Then inherited the Abs2 abstract in the class Sample. Here we override the methods of both abstract classes.

Now look to the Program class, It contains the Main() method, the Main() method is the entry point for the program. Here, we created the references of both abstract classes that are initialized with the object of the Sample class and then called the Method1() and Method2() that will print the appropriate message on the console screen.

C# Basic Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.