C# program to demonstrate the inheritance of abstract classes

Here, we are going to demonstrate the inheritance of abstract classes in C#.
Submitted by Nidhi, on October 26, 2020

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

Program:

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();

        M1.Method1();
        M2.Method2();
    }
}

Output:

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

Explanation:

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.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.