Powered by Blogger.

Monday, August 2, 2010

What is abstract in C#

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.
Syntax

public abstract return-type method-name(parameter list);
Example
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.


Syntax

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("Name:-{0}",name);

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

  s.Salary();

  s.Show();

 }

}
by Abhisek 

 



 

No comments:

Post a Comment

  ©Template by Dicas Blogger.

TOPO