[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 322
  • Last Modified:

In C# development when should i choose abstract class over ordinery class.

Hi,

In C# development when should i choose abstract class over ordinery class.
actually i gone through google and iam not able to find the solution.
pls provide me the example when i need to go for abstrat classs.
With Regards
Ravi
0
mu_ravi1
Asked:
mu_ravi1
1 Solution
 
Imran Javed ZiaCommented:
Hi,

If you have a situation where you need some basic functionaility common in all classes then it is time to go for inheritence like you need common properties like name, address, dbo and etc, and in some other case where you have need to have createdby, created date and etc common in all classes.

on the other hand when it is about making a class Abstract, it is can be based on many thing specially when you are interested in polymorphism and there is predefined set of instructions/action which must be implemented by every class like for a typical CRUD application then you may define Create/Add, Get/Read, Update and Delete methods.

for more information please review following links:
http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx
http://social.msdn.microsoft.com/Forums/en/ieextensiondevelopment/thread/222f1a3c-58c5-42a6-bb58-df53d8055386
http://en.csharp-online.net/Should_I_use_an_abstract_class_or_an_interface%3F
http://www.codeproject.com/KB/cs/jmabstractclasses.aspx

Thanks
0
 
AndyAinscowCommented:
Further to the information from IJZ (which is very useful)

An abstract class can NOT have an object of that type.  eg.  If class CAbstract is abstract then the following will not work:
CAbstract obj = new CAbstract();

You can only create objects on classes derived from CAbstract that are themselves not an abstract class.
0
 
NavneetCommented:
Hi!
Abstract is a basic templete with basic features and basic requirements you would like to have atleast in all your Classes.
In more simple terms it's having a Templete which you can't sell(create object), but can produce objects from it to sell.

Thanks!
0
 
Mahmoud_Al_HattabCommented:
well all the above is very useful. Here is an explained example to clarify more:
1- You have the You are making a drawing application. The base interface is IShape:
interface IShape
{
 void Draw();
 double Area();
}
The interface defines a contract for all users that this object(that implements IShape) does support the method Draw.
2- Now the rectangle and square have common implementations in the area. Thus lets have the abstract class:
abstract class FourRightSidesShape : Ishape
{
 public double Length{get;set;}
 public virtual double Width{get;set;}
 public double Area()
 {
  return this.Length * this.Width;
 }

 public abstract void Draw();
//further methods can be added as well even if not from the bas interface.. they can be either abstract or have implementation.
}

Now notice that you can not create a FourRightSidesShape obbject. You should inherit a Rectangle or Square from FourRightSidesShape:

class Square : FourRightSidesShape
{
 public override double Width
 {
  get{return this.Length;}
  set{this.Length = value;}
 }

 public void Draw()
 {//drawing code goes here...
 }
}

Now you can instantiate Square object. No nee to repeat the Area method for the Square and Rectangle. Just the Draw method.

Shortly, the abstract is like the interface except for that:
 1- it can/may contain implementation for methods and properties.
 2- it may/can contain variables.
 3- another third non functional distinction: it is that it can have protected and/or private members(methods, properties, variables... etc). Either abstract methods or with their implementations.

 Well, if you have any further inquiries, please do not hesitate :)

regards,
Mahmoud Al-Hattab
0
 
mu_ravi1Author Commented:
not suffcient
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now