Q:

C# program to demonstrate the inheritance of abstract classes

0

C# program to demonstrate the inheritance of abstract classes

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to implement hierarchical inheritance u... >>
<< C# program to demonstrate the simple example of th...