The abstract class is a special type of class which contains at least one abstract method. It should be declared with the keyword abstract. Every abstract class consists of one or more abstract methods. These methods will only have the method definitions i.e. abstract class will not have any method body, such as the instance method or the class method. | ||||||||||||||||||||||||
Basically, a base class is declared with the abstract keyword, and the derived classes should inherit the abstract class and provide implementation for the relevant methods. In the given example of abstract class, an abstract method named Display() is declared inside the AbstractClass class. | ||||||||||||||||||||||||
The derived class AbstractMethodDef then inherits the abstract class AbstractClass and provides implementation to the abstract method. Inside the Main() method, an instance of the class is created and the method is called. | ||||||||||||||||||||||||
AbstractClass.cs | ||||||||||||||||||||||||
using System; abstract public class AbstractClass { public abstract void Display(); } class AbstractMethodDef:AbstractClass { public override void Display() { Console.WriteLine("Abstract Method Implemented"); } public static void Main() { AbstractMethodDef abs = new AbstractMethodDef (); abs.Display(); } } |
||||||||||||||||||||||||
Difference between Abstract Class and Interface | ||||||||||||||||||||||||
There are some similarities and differences between an interface and an abstract class that have been arranged in the given table for easier comparison: | ||||||||||||||||||||||||
|
Tags Object Oriented Concept in C#
Check Also
Exceptions In C#
System.Exception is the base class for all other exceptions. Below this level are the System.ApplicationException …