Showing posts with label inheritance. Show all posts
Showing posts with label inheritance. Show all posts

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.

Saturday, January 8, 2011

Object Oriented Programming (OOP)

Object Oriented Programming (OOP) 
Object oriented programming (OOP) deals with thing called 'objects'. Objects are just extremely functional ways of organizing information. Object in an OOP language are combinations of code and data that are treated as single unit.

Understanding Whole Mechanism of OOP
To see object oriented working in action, consider your everyday life. The 'real world' is full of objects. Take a Door for an example, the door needs to be opened and closed, and you pass message by turning its knob. The same message (like turning knob) will have different meanings when you apply to different object. If you turn the knob of radio it will act differently than door, same as with window and desk. But all of them have the same function called 'open' but different behaviour. A new door with a stained glass window inherits most of its quality from a standard door. When you use a door, you usually do not care about the latch or hinge, construction features, and you don't have access to the interior working of the knob or what may be written on the inside of the door panel.
Basic principles of object oriented programming are