C# Simplified, part 4: Structures, Inheritance and Interfaces - Abstract classes
(Page 5 of 7 )
The abstract class is a special type of class which contains one or more abstract methods. 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. The 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 listing 4.5, an abstract method named Display() is declared inside the AbstractDemo class. The derived class then inherits the abstract class and provides implementation to the abstract method. Inside the Main() method, an instance of the class is created and the method is called.
Listing 4.5
using System;
abstract public class AbstractDemo
{
public abstract void Display();
}
class AbstractImp:AbstractDemo
{
public override void Display()
{
Console.WriteLine("Abstract Method Implemented");
}
public static void Main()
{
AbstractImp abs = new AbstractImp();
abs.Display();
}
}
Next: Interfaces >>
More C# Articles
More By Anand Narayanaswamy