C# Abstract class
• Abstract classes are the way to achieve abstraction in
C#. Abstraction in C# is the process to hide the internal
details and showing functionality only.
• A method which is declared abstract and has no body is
called abstract method. It can be declared inside the
abstract class only. Its implementation must be
provided by derived classes.
• A user must use the override keyword before the
method which is declared as abstract in child class,
the abstract class is used to inherit in the childclass.
An abstract class cannot be inherited by structures. It
can contains constructors or destructors. It can
implement functions with non-Abstractmethods.
• SYNTEX
public abstract void method();
FOR EXAMPLE
using System;
public abstract class Shape
{
public abstract void draw();
}
public class Rectangle : Shape
{
public override void draw()
{
Console.WriteLine("drawing rectangle...");
}
}
public class Circle : Shape
{
public override void draw()
{
Console.WriteLine("drawing circle...");
}
}
public class TestAbstract
{
public static void Main()
{
Shape s;
s = new Rectangle();
s.draw();
s = new Circle();
s.draw();
}
}
Output:
drawing ractangle...
drawing circle...