C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Abstract Classes and Abstract Method(s)


Posted Date: 09 Aug 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Gaurav Arora       Member Level: Gold
Rating:     Points: 10



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#




Responses

Author: Geetha    13 Aug 2008Member Level: Gold   Points : 1
Hi

thanks a lot for detailed sharing about abstarct class.
Keep it up.

Thanks,
Geetha.


Author: Gaurav Arora    13 Aug 2008Member 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


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Questions  .  Interview  .  Defnitions  .  Abstract Method  .  Abstract Classs  .  Abstract Classes in C#  .  Abstract Classes  .  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: Constructors and Destructors in C#
Previous Resource: Custom Exception and Logging
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

teleconferencing

Contact Us    Privacy Policy    Terms Of Use