C# program to demonstrate abstract class with multiple-level inheritance

Here, we are going to demonstrate abstract class with multiple-level inheritance in C#?
Submitted by Nidhi, on October 26, 2020

Here, we will create an abstract class and implement the abstract method in a parent class and then implement multi-level inheritance.

Program:

The source code to demonstrate abstract class with multiple-level inheritance is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate abstract class 
//with multi-level 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 : Sample2
{
    //Method definition
    public void Method3()
    {
        Console.WriteLine("Method3() called");
    }
}

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

        S.Method1();
        S.Method2();
        S.Method3();
    }
}

Output:

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

Explanation:

Here, we created an abstract class Abs that contains an abstract method Method1(). Then we override the Method1() in the Sample1 class. After that, we created multi-level inheritance with the help of Sample1, Sample2, and Sample3 classes. Each class contains one method.

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 object of Sample3 class and called all methods 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.