Q:

C# program to implement the same abstract method in multiple classes

0

C# program to implement the same abstract method in multiple classes

All Answers

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

Program:

The source code to implement the abstract method with the same name in multiple classes is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to implement the same abstract method 
//in multiple classes.

using System;

abstract class Abs
{
    //Method Declaration
    public abstract void Method();
}

class Sample1 : Abs
{
    //Method definition
    public override void Method()
    {
        Console.WriteLine("Sample1:Method() called");
    }
}

class Sample2 : Abs
{
    //Method definition
    public override void Method()
    {
        Console.WriteLine("Sample2:Method() called");
    }
}


class Program
{
    public static void Main(String[] args)
    {
        Abs M;

        M = new Sample1();
        M.Method();

        M = new Sample2();
        M.Method();
    }
}

Output:

Sample1:Method() called
Sample2:Method() called
Press any key to continue . . .

Explanation:

Here, we created an abstract class Abs that contains the declaration of method Method(). Then we created two classes Sample1Sample2. Then we implemented the Method() in both 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 reference M of Abs abstract class and then initialized M by objects of all classes one by one and call implemented methods that will print corresponding messages 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 inherit an abstract class and interf... >>
<< C# program to demonstrate the inheritance of inter...