You are on page 1of 2

Abstraction means that you have some class that is more common than others that extend it.

By example, if you have classes Triangle and Rectangle:

class Triangle { public double a; public double b; public double c;

public double Area { get { return triangle's area } } }

class Rectangle { public double a; public double b;

public double Area { get { return rectangle's area } } } These classes have something in common - they have property called Area plus also sides (a and b) but I don't see any reason to make sides abstract. Let's add also Circle and we have no point of sides at all. Now let's generalize these classes and let's create one more common class called Shape. Also, let's make it abstract class so this class cannot be created separately - it can be created only through extending. public abstract class Shape { public double Area(); } And now let's write previous classes so they extend Shape class. class Triangle : Shape { public double a; public double b; public double c; public double Area

{ get { return triangle's area } } } class Rectangle : Shape { public double a; public double b; public double Area { get { return rectangle's area } } } So, what's the win, you may ask? Okay, not of these classes use Shape as their base class. So does Circle. In the context where we don 't care about specific properties of object we can handle all extended objects as Shape. By example, let's calculate total area of Shapes in list. List<Shape> shapes = ListShapes() // contains circles, triangles and rectangles double area = 0; foreach(Shape shape in shapes) area += shape.Area; // do something useful with area here Without extending Shape class we should write separate loop for each shape type. And also we have to have separate methods to get different types of shapes. Now we had only one method to list shapes and one loop to handle them as one - we didn't cared about lengths of sides or radiuses or any other specific properties. Same way you can make many other abstractions. By example, you can add coordinates to Shape class if shape's positioning is required.

You might also like