Q:

C# program to implement hierarchical inheritance using the abstract class

0

C# program to implement hierarchical inheritance using the abstract class

All Answers

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

Program:

The source code to implement hierarchical inheritance using abstract class is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate abstract class 
//for hierarchical inheritance.

using System;

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

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

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

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

class Program
{
    public static void Main(String[] args)
    {
        Sample2 S2 = new Sample2();
        Sample3 S3 = new Sample3();

        S2.Method1();
        S2.Method2();

        S3.Method1();
        S3.Method3();
    }
}

Output:

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

Explanation:

Here, we created an abstract class Abs that contains the abstract method Method1(). Then we inherited the abstract class Abs in the Sample1 class.  After that Sample1 class is inherited by Sample2 and Sample3 classes. The Sample2 and Sample3 classes also contain extra methods.

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 objects of Sample2 and Sample3 classes. Then we called all methods that will print the corresponding 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 demonstrate the use of reflection to... >>
<< C# program to demonstrate the inheritance of abstr...