doubt on abstration concept

R

rajendra

hi friends,
i have placed code because i want to explain my doubt clearly to
you ,and i have placed my doubt in Main of the program (static void
Main(string[] args)) by // before that





using System;

namespace AbstractClasses
{
abstract class Shape
{
public abstract double GetArea();
public abstract double GetPerimeter();
}
class Circle : Shape
{
public double Radius;
public override double GetArea()
{
return Math.PI * Radius * Radius;
}
public override double GetPerimeter()
{
return 2 * Math.PI * Radius;
}
}
class Rectangle : Shape
{
public double Width, Height;
public override double GetArea()
{
return Width * Height;
}
public override double GetPerimeter()
{
return 2 * (Width + Height);
}
}
class Class1
{
static void Main(string[] args)
{
Shape s = new Shape();
Shape [] shapes = new Shape[2];
Circle c = new Circle();
c.Radius=5;
Rectangle r = new Rectangle();
r.Width=8;
r.Height=6;
shapes[0]=c;//what we can achiev by assigning object c to

shapes reference ,is there any special use in

having abstract class like this [donot consider

this example alone]
shapes[1]=r;
double total = 0;
foreach (Shape s in shapes)
total += s.GetArea();
Console.WriteLine("The total area is {0}", total);
}
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

shapes[0]=c;//what we can achiev by assigning object c to

shapes reference ,is there any special use in

having abstract class like this [donot consider

this example alone]
shapes[1]=r;
double total = 0;
foreach (Shape s in shapes)
total += s.GetArea();
Console.WriteLine("The total area is {0}", total);

Well you example exemplify that clearly. You can calculate the area of any
figure you have define without knowing the real type of the figure, why?
because you know that all figures will implement the methods that your base
class (Shape) defined.
But in the same hand you do not want an instance of Shape created.
 
B

Brian Gideon

what we can achiev by assigning object c to
shapes reference ,is there any special use in
having abstract class like this [donot consider
this example alone]

Like Ignacio said that was the perfect example. But other examples
include plugin architectures where you can treat different plugins as
being similar enough to get them initialized and executing.

You can also get this behavior by using interfaces. Consider an
expression evaluator.

Compiler compiler = new Compiler();
IExpression expression = compiler.Compile("sin(3) * log(5)");
Console.WriteLine(expression.Evaluate());

In the above example the Compile method just returns an object that
implements IExpression. The actual concrete type it returns could be
anything. It could be an AddExpression, or SinExpression, or some
other type that can represent any arbitrarily complex expression. The
neat thing about it is that *all* expressions are treated the same
way. That is, they all have the Evaluate method and can thus be
evaluated without knowing the actual type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top