.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Abstract Classes

Posted By:Shashi Ray       Posted Date: February 23, 2009    Points: 15    Category: C#    URL: http://www.dotnetspark.com  
 

Abstract Classes

 

An abstract class declares one or more methods but leaves them unimplemented. If you declare a method as abstract, you must also declare the class as abstract. Suppose, for example, that we define a base class called Shape. It will save some parameters and create a Pen object to draw

with. However, we'll leave the actual draw method unimplemented, since every different kind of shape will need a different kind of drawing procedure:

 

public abstract class Shape {

protected int height, width;

protected int xpos, ypos;

protected Pen bPen;

//-----

public Shape(int x, int y, int h, int w) {

width = w;

height = h;

xpos = x;

ypos = y;

bPen = new Pen(Color.Black );

}

//-----

public abstract void draw(Graphics g);

//-----

public virtual float getArea() {

return height * width;

}

}

 

Note that we declare the draw method as abstract and end it with a semicolon rather than including any code between braces. We also declare the overall class as abstract. You can't create an instance of an abstract class like Shape, though. You can only create instances of derived classes in which the abstract methods are filled in. So, lets create a Rectangle class that does just that:

 

public class Rectangle:Shape {

public Rectangle(int x, int y,int h, int w):

base(x,y,h,w) {}

//-----

public override void draw(Graphics g) {

g.DrawRectangle (bPen, xpos, ypos, width, height);

}

}

 

This is a complete class that you can instantiate. It has a real draw method. In the same way, we could create a Circle class which has its own draw method:

 

public class Circle :Shape {

public Circle(int x, int y, int r):

base(x,y,r,r) { }

//-----

public override void draw(Graphics g) {

g.DrawEllipse (bPen, xpos, ypos, width, height);

}

}

 

Now, if we want to draw the circle and rectangle, we just create instances of them in the init method we call from our constructor. Note that since they are both of base type Shape we can treat them as Shape objects:

 

public class Form1 : System.Windows.Forms.Form {

private PictureBox pictureBox1;

private Container components = null;

private Shape rect, circ;

//-----

public Form1() {

InitializeComponent();

init();

}

//-----

private void init() {

rect = new CsharpPats.Rectangle (50, 60, 70, 100);

circ = new Circle (100,60, 50);

}

 

Finally, we draw the two objects by calling their draw methods from the paint event handler we create as we did above:

 

private void pictureBox1_Paint(object sender, PaintEventArgs e) {

Graphics g = e.Graphics ;

rect.draw (g);

circ.draw (g);

}

 

Shashi Ray


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend