C# program to demonstrate the simple example of the abstract class

Here, we are going to demonstrate the simple example of the abstract class in C#?
Submitted by Nidhi, on October 14, 2020

Here, we will demonstrate the simple example of an abstract class; here we will implement abstract methods into a class.

Program:

The source code to demonstrate the simple example of abstract class is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate the simple example 
//of the abstract class.

using System;

abstract class AbsClass
{
    //Method Declaration
    public abstract void Method1();
    public abstract void Method2();
    public abstract void Method3();
}

class Sample : AbsClass
{
    //Method definitions
    public override void Method1()
    {
        Console.WriteLine("Method1() called");
    }
    public override void Method2()
    {
        Console.WriteLine("Method2() called");
    }
    public override void Method3()
    {
        Console.WriteLine("Method3() called");
    }

    public static void Main(String[] args)
    {
        AbsClass M = new Sample();

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

Output:

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

Explanation:

Here, we created an interface AbsClass that contains three abstract methods Method1(), Method2(), and Method3().

Here, we also created a class Sample that inherited the abstract class AbsClass. Here we defined the methods Method1(), Method2(), and Method3(). The Sample class also contains the Main() method. The Main() method is the entry point for the program. Here we created the reference of the AbsClass abstract class and initialized with the object of Sample class and called all three methods, that will print appropriate messages 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.