.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Kaviya Balasubramanian
Sgraph Infotech
Imran Ghani

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Abstract Method and Abstract Class

Posted By:Abhisek Panda       Posted Date: October 27, 2009    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

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);

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend