We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

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

mu_ravi1
mu_ravi1 asked
on
Medium Priority
340 Views
Last Modified: 2013-12-16
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
Comment
Watch Question

Consultant Software Engineer - .NET Architect
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
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.
Navneet.Net Full Stack Developer

Commented:
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!
Mahmoud_Al_HattabSoftware Architect
CERTIFIED EXPERT

Commented:
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

Author

Commented:
not suffcient
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.