C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Abstract Method and Abstract Class


Posted Date: 05 Oct 2009    Resource Type: Articles    Category: .NET Framework
Author: Abhisek PandaMember Level: Gold    
Rating: 1 out of 5Points: 10



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. In this case it will create a child and the same process will continue.

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.

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



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Abstract Method and Abstract Class  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Interface in C#
Previous Resource: Masked Edit Control using framework & C#.net
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use