.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

Using Inheritance

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

Using Inheritance

 

Inheritance in C# gives us the ability to create classes which are derived from existing classes. In new derived classes, we only have to specify the methods that are new or changed. All the others are provided automatically from the base class we inherit from. To see how this works, lets consider writing a simple Rectangle class that draws itself on a form window. This class has only two methods, the constructor and the draw method.

 

namespace CsharpPats

{

public class Rectangle {

private int x, y, w, h;

protected Pen rpen;

public Rectangle(int x_, int y_, int w_, int h_)

{

x = x_; //save coordinates

y = y_;

w = w_;

h = h_;

//create a pen

rpen = new Pen(Color.Black);

}

//-----------------

public void draw(Graphics g) {

//draw the rectangle

g.DrawRectangle (rpen, x, y, w, h);

}

}

}

 

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