Abstract Method and Abstract Class

Posted by in C# category on for Beginner level | Views : 10126 red flag
Rating: 3.5 out of 5  
 2 vote(s)

This article describes about Abstract class and methods in C# with examples.
Abstract Method

Abstract method is just the opposite of Final method. Final method must not overridden by its child, whereas Abstract method have to be overridden by the child. It is just like the pure virtual function of C++.

It is declared by the keyword abstract. It has no body part. Abstract method must be declared inside an Abstract class, but the vice verse is not correct. This means that it is not necessary that a abstract class always contain abstract methods. Abstract method's definition appears in the child.

It is just like the skeleton of the program and act as a reminder. That means it is defined once in the super class and again used in the derived class.

In C# the abstract method must be declared as public and it is mandatory that the abstract method must be overridden in the child class. If it is not overridden by child compiler generates error.


public abstract return-type method-name(parameter list);

public abstract void Get(int a,int b);
Abstract Class

The class declared by the keyword abstract is called as an Abstract Class. Usually abstract classes contain abstract methods, but it is not mandatory. If a class contains abstract method then it is mandatory for that class to be abstract class.

Abstract class can not be instantiated. If there is a abstract class there must be a child to override the abstract methods of abstract class.

Child have to override all the abstract methods. If not or partially, then it should be abstract class again.

Abstract class is just like the skeleton of the program. In an inheritance hierarchy, usually top classes are declared as abstract to make a program more reliable. In this case it will create a child and the same process will continue.


abstract class class-name
Data Members;
General methods
Abstract Methods;

See the sample program to better understand abstract class and method. The following program calculates the net salary of an employee.

//Abstract class
abstract class Demo
protected string name;
protected int id;
public void Get(string n , int i)
name = n;
id = i;
//Defining Abstract methods
public abstract void Set(string des , double sal);
public abstract void Salary();

class Salary:Demo
string designation;
double netsalary , salary;

//Overriding Abstract methods
public override void Set(string des , double sal)
designation = des;
salary = s;

public override void Salary()
netsalary = salary + salary * 0.25;

public void Show()
Console.WriteLine("ID:-{1}", id);
Console.WriteLine("Designation:-{2}" , designation);
Console.WriteLine("Salary={3}" , salary);
Console.WriteLine("Net Salary={4}" , netsalary);

class MainClass
static void Main(string args[])
Salary s = new Salary();
s.Get("Abhisek Panda" , 108);
s.Set("Analyst" , 50000);
Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Abhisek Panda
Member Level: Bronze
Member Status: Member
Member Since: 10/11/2009 6:25:59 AM
Country: India
Abhisek Panda

Thanks and Regards Abhisek Panda Go Green And Save Your Future

Login to vote for this post.

Comments or Responses

Posted by: Nav234 on: 8/20/2010 | Points: 10
Hi Abhisek,

That was a good article ,but
in the sample code provided for calculating employee salary,there must be a small error
while compiling.
"'Salary': member names cannot be the same as their enclosing type"
public override void Salary()
netsalary = salary + salary * 10;

thanks in advance

Login to post response

Comment using Facebook(Author doesn't get notification)