Friday, January 14, 2011

Inheritance

Inheritance
Inheritance is a process of creating new classes from previous defined classes.
Inheritance is the addition of behaviour (i.e. methods) and attributes (i.e. variables) of a base class in a derived class so that they are accessible in that derived class. The key benefit of Inheritance is that it provides the formal mechanism for Code reusability. In c# we can inherit only one class at a time but can inherit many interfaces.
The existing class is called the parent class and the derived class is called the child class. We can also say that Inheritance is the process in which the derive class inherit or receive characteristics from base class same as a child inherits characteristics from their parents. 
When a derive class is inherits from base class, then derive class contains all the behaviour (i.e. methods) and attributes (i.e. variables) of the base class and derive class adds some new characteristics of its own. If base class makes any changes then it’s propagated to all derive class that inherit from them. Unless explicitly overridden. 

Generalization and Specialization are two terms used in inheritance. clsShapes is generalize form and clsLine, clsTriangle and clsCircle are specialize form of clsShapes. Because they also have there own special methods which makes them different from another class. Generalization is from bottom to top and Specialization is from top to bottom.

We can inherit our class from classes as well as interface:

Inheritance between classes is called Implementation inheritance
Inheritance between classes and interface is called Interface inheritance


Implementation Inheritance
In implementation inheritance we inherit class from class. I am made a class clsShapes which is base class and clsLine, clsCircle and clsTriangle are derived class of clsShaped (inherited from clsShapes).

In above picture I have made a class clsShapes which contains draw method and class clsCircle, clsLine and clsTriangle which have fill method and is inherited from class clsShapes.

Here you can see I have made an object of clsCircle which is inherited from class clsShapes. objcircle is object of clsCircle. When you write objcircle(.) then you will see all those variables and methods which clsCircle have like in clsCircle we have draw and fill method. Draw method was not in clsCircle but we have inherit clsShapes that's why we can use draw method in clsCircle.

Interface inheritance
Now we will inherit class with interfaces.Take the same example we use in implementation inheritance. I have made a interface named Ishapes and its contain fill method. clsCircle and clsTriangle are inherited from Ishapes.


Now when we instantiate object of clsCircle or clsTriangle we can use fill methods and we can use same as we have used fill method in implementation inheritance. but in interface we don't have to write methods everytime in every class we just make a interface and inherit it and then use it by giving body of all that method which are in interface.

No comments:

Post a Comment