Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » .NET Framework »
Abstract Classes
|
Abstract Class:
A class that provides common behavior across a set of subclasses, but is not itself designed to have instances that work.
Why Abstract Class?
If you would like to make classes that only represent base classes, and don’t want anyone to create objects of these class types.
OR
It is used to indicate that a class is incomplete and that it is intended to be used only as a base class.
Some important points to remember:
· An abstract class cannot be instantiated. · An abstract class is permitted (but not required) to contain abstract members. · An abstract class cannot be sealed. · An abstract method cannot be private. · If you declare an abstract method as protected, it should be protected in its derived class. · An abstract method cannot have the modifier virtual, because an abstract method is implicitly virtual. · An abstract member cannot be static
Examples An example of an abstract class declaration is:
Abstract class absClass { May contain some abstract members, methods. }
An abstract class can contain either abstract methods or non abstract methods. Abstract members do not have any implementation in the abstract class, but the same has to be provided in its derived class.
An example of an abstract method:
abstract class absClass { public abstract void abstractMethod(); }
Sample Program 1:
using System;
namespace abstractSample { //Creating an Abstract Class abstract class absClass { //A Non abstract method public int AddTwoNumbers(int Num1, int Num2) { return Num1 + Num2; }
//An abstract method, to be //overridden in derived class public abstract int MultiplyTwoNumbers(int Num1, int Num2); }
//A Child Class of absClass class absDerived:absClass { [STAThread] static void Main(string[] args) { //You can create an //instance of the derived class
absDerived calculate = new absDerived(); int added = calculate.AddTwoNumbers(10,20); int multiplied = calculate.MultiplyTwoNumbers(10,20); Console.WriteLine("Added : {0}, Multiplied : {1}", added, multiplied); }
//using override keyword, //implementing the abstract method //MultiplyTwoNumbers public override int MultiplyTwoNumbers(int Num1, int Num2) { return Num1 * Num2; } } }
In the above sample, you can see that the abstract class absClass contains two methods:
AddTwoNumbers is a non-abstract method which contains implementation and MultiplyTwoNumbers is an abstract method that does not contain implementation.
The class absDerived is derived from absClass and the MultiplyTwoNumbers is implemented on absDerived. Within the Main, an instance (calculate) of the absDerived is created, and calls AddTwoNumbers and MultiplyTwoNumbers. You can derive an abstract class from another abstract class. In that case, in the child class it is optional to make the implementation of the abstract methods of the parent class. Sample Program 2: //Abstract Class1 abstract class absClass1 { public abstract int AddTwoNumbers(int Num1, int Num2); public abstract int MultiplyTwoNumbers(int Num1, int Num2); }
//Abstract Class2 abstract class absClass2:absClass1 { //Implementing AddTwoNumbers public override int AddTwoNumbers(int Num1, int Num2) { return Num1+Num2; } }
//Derived class from absClass2 class absDerived:absClass2 { //Implementing MultiplyTwoNumbers public override int MultiplyTwoNumbers(int Num1, int Num2) { return Num1*Num2; } }
In the above example, absClass1 contains two abstract methods AddTwoNumbers and MultiplyTwoNumbers. The AddTwoNumbers is implemented in the derived class absClass2. The class absDerived is derived from absClass2 and the MultiplyTwoNumbers is implemented there. Sample Program 3: Abstract properties example: //Abstract Class with abstract properties abstract class absClass { protected int myNumber; public abstract int numbers { get; set; } }
class absDerived:absClass { //Implementing abstract properties public override int numbers { get { return myNumber; } set { myNumber = value; } } }
In the above example, there is a protected member declared in the abstract class. The get/set properties for the member variable myNumber is defined in the derived class absDerived.
|
Responses
|
| Author: V.Venkatesa Prasath 13 Oct 2005 | Member Level: Bronze Points : 0 | Very Nice. Thanks & Will , Prasath VV From Coimbatore.
| | Author: Gaurav Arora 21 Feb 2009 | Member Level: Diamond Points : 1 | Hi,
Its a good one and you are doing good job.
Keep posting articles.
Gaurav
|
|