Oct 21, 2011


Inheritance in C #. Conceptand Examples

Likeall current programming language, C # is an object oriented language andsupports all the features of the paradigm of object-oriented programming:encapsulation,inheritance and polymorphism.
In thispost I will try to explain as simply as possible, which is the heritageprogramming and put some examples at the end to see if they carry the idea,although I'm sure with some doubts remain as it is an easy concept understand,but difficult to implement in programming ... then we'll talk then what is theencapsulation and polymorphism
Bydefinition, the inheritance is not more than the ability of an object toinherit the characteristics of another. Well, programming, inheritance isthe ability of a class inherit the methods, variables and properties of another(the constructors are not inherited).This saves code, and that several featureswould not have to deploy again.
A classthat inherits from another, you can use the methods, properties or variables ofthe parent class. You can also redefine or modify the methods andproperties, using some reserved words for this, creating a new object behavior. Anecessary condition for a class inherits from another, is that the child class(which inherits) should be used where you use the parent class (which willinherit). If at least one case where you can use the parent class, and youcan not use the child class, inheritance does not make sense.
In C #,a class can only inherit from another (for some reason difficult to explain). Thatis, a class can not inherit from several classes at once. For a classinherits from another is just to put ' : 'after his return,and continued to put the name of the class you want to inherit.
Example:
class Circle : Figure
 {
    / / Implementation of class
}
If achild class has a constructor thatreceives the same parameters as a parent class's constructor and thisconstructor performs the same functions in the parent class, the parent classcan be called from the child class after putting statement of the child classconstructor: and after the keyword base. Within the parentheses are basedconstructor parameters (also see an example).
It isalso important to know that if a class inherits from another, which in turninherits from another, this class also inherits from the class above.
Forexample, if a quadrilateral is a figure and a parallelogram is a quadrilateral,then a parallelogram is a figure ...
For aparent class let one of their methods is redefined by its inherited classes(which inherit from it) this method must be declared using the keyword virtual ,and when the child class will redefine it, be declared using the reserved word override .
Example:
public <strong> virtual </ strong> void Method ()/ / Method in the class declaration of the Father{/ / Code goes here} public override void Method ()/ / Method in the class declaration of the Son{/ / New INSTRUCTIONS}
  public override void Method ()/ / Method in the class declaration of the Son{/ / New INSTRUCTIONS} 
If achild wants to redefine a class from a parent class method, and this was notdeclaredvirtual in the parent class, then you have to put thereserved word new in the statement of the method.
Example:
public  void Method ( )
/ / method declaration in the class Parent
{
   / / code goes here
}

public  new void Method ( )
/ / method declaration in class Child
{
   / / new instructions
}
Exampleof inheritance
public  class Child : Parent
 {

/ / child class inherits from the parent class
public Child ( int n ) : base ( n ) { }
}
TheChild class's constructor that receives as parameters an integer called theParent class constructor passing it receives an integer parameter.
Now wewill see a real example where you see the importance of this importantmechanism. Imagine, we want to have some geometric figures to calculatethe area, perimeter, moving in a plane, etc.. But we soon realize that allthe figures have area and perimeter, only to be calculated in different ways. Allcan hide, show and move the same way, no? Consider now the code to followthis idea:
Beforeturning to the code, you must define a class hierarchy is a group of classesthat inherit from each other, where there is a base class which inherits allthe members of the hierarchy, in this case, the base class is Figure class.
/ / Declare the parent class, which inherit all the figures
/ / It is abstract because it will have some methods abstract

public abstract class Figure
 {
/ / We use <strong> protected, </ strong> to only haveaccess
/ / to these variables , classes that inherit from it
protected  int x, y ;
protected Control control ,
protected myPen Pen ;
protected  bool visible ;

/ / A default constructor for all figures
public figure ( int x, int and Color color, Control control )
{
/ / Validate


 




 
 

 

  
  
  
  
  
  
Now wewill code two other figures namely that class inherit figure, a circle and arectangle.
public  class Circle : Figure
 {
/ / We see that in this class only need to declare
/ / a variable
protected  int radius ;
/ / This constructor will have all the parameters
/ / by default and it is a circle radius
public Circle ( int x, int and miColor Color, Control mycontrol,
  int radius )
: base ( x, y, miColor,mycontrol )
{
   / / Validation
   this . radius  = radius ;
}
/ / Now we have set as the figure shows
public  override void Display ( )
{
   Graphics area as calculated in this case public override int Area ( ) { return Math. IP * Math. PI * radius ; } / / We can add another independent method public int Perimeter ( ) { return 2 * Math. PI * radius ; } } / / This class alsoinherits figure public class Rectangle : Figure
 { protected int width, height ; public Rectangle ( int x, int and miColor Color,Control
as show the figure publicoverride void Display ( ) {
   Graphics g = control. CreateGraphics ( ) ;
   g. DrawRectangle ( myPen, x, y, width, height ),
   visible = true ; } / / calculate the area redefined as public override int Area ( ) { return high * width ; } }



 

    





      





   




  
   
   


 



 

  

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes