The abstract classe is a design concept, which means they cannot be implemented directly and are meant only to be as base classes. You cannot create an instance of an abstract class:
Characteristics of Abstract Class - You can not create an object of abstract class.
- Abstract class is design to act as a base class.
- Abstract classes are similar to interfaces. After declaring an abstract class, it cannot be instantiated on its own, it must be inherited.
- In C# you can create an abstract class by using abstract keyword.
- Abstract classes can have pure abstract method.
- If any class has at least one abstract method then it should be defined and implemented as an abstract class.
On the other hand, abstract methods are virtual methods which are implemented and sign in derived or inherited class. Abstract methods are only defined in abstract class but implementation is done only in the derived class. As these are virtual, declaring without the keyword virtual but these are implemented using override keyword in derived class.
The following Code sample implements Abstract Class and describes the complete view of Abstract class and Abstract method.
/* This Example is a part of different * examples shown in Book: * C#2005 Beginners: A Step Ahead * Written by: Gaurav Arora * Reach at : gaurav.aroraose@yahoo.co.in*/
// File name : abstractclass.cs
using System;
namespace CSharp.AStepAhead.abstractclass { class mainClass { public static void Main() { //Create a class object childClass objClass = new childClass(); int num1, num2; Console.Clear(); objClass.getInfo(); Console.WriteLine("\n This is non-abstract method: "); Console.Write("\n Enter First Number: "); num1 = int.Parse(Console.ReadLine()); Console.Write("\n Enter Second Number: "); num2 = int.Parse(Console.ReadLine()); Console.Clear();
objClass.showInfo(); Console.WriteLine("\n This is non-abstract method: "); Console.WriteLine(" Num1 : {0}, Num2 : {1} Sum : {2}", num1, num2, baseClass.add(num1,num2));
Console.ReadLine(); Console.Clear(); }
} abstract class baseClass { public abstract void getInfo(); //no sinature here public abstract void showInfo(); //no sinature here
public static int add(int num1, int num2) // non abstract method { return num1 + num2; } } class childClass : baseClass { string name, fname; char sex; int age;
public override void getInfo() { Console.Write("Please Enter Your Name: "); name = Console.ReadLine(); Console.Write("\nPlease Enter Your Father Name: "); fname = Console.ReadLine(); Console.Write("\nPlease Enter Your Sex[male-m female-f]: "); sex = Convert.ToChar(Console.ReadLine()); Console.Write("\nPlease Enter Your Age: "); age = int.Parse(Console.ReadLine()); }
public override void showInfo() { Console.WriteLine("\n\n You have provided following information(s): \n"); Console.WriteLine(" Name :{0}\n Father Name : {1}\n Age : {2}\n Sex : {3}", name, fname, age, sex); }
}
}
Difference between abstract classes and interfaces - Abstract classes can have concrete methods while interfaces have no methods implemented.
- Interfaces do not come in inheriting chain, while abstract classes come in inheriting chain.
- Interfaces have only pure virtual abstract method, while abstract classes may have non-abstract methods.
- All members are public [by default] in interfaces but you have to declare public members in abstract classes.
For more details, visit http://www.msdotnetheaven.com/ChaptersCsharp/csharplanguageii.htm#
|
| Author: Geetha 13 Aug 2008 | Member Level: Gold Points : 1 |
Hi
thanks a lot for detailed sharing about abstarct class. Keep it up.
Thanks, Geetha.
|
| Author: Gaurav Arora 13 Aug 2008 | Member Level: Gold Points : 1 |
Hi Geetha,
Thanks a lot for your words. These will pump me with more energy to share my technical stuff.
Please do encourage me in future.
Thanks & regards, Gaurav Arora
|