Tuesday, March 17, 2015

OOPs Concepts .

# Object Oriented Programming : OOP is a method of programming.The main purpose of object oriented programming is that it simplify the design, coding by providing re-usability and readability and also debugging of a program is easier.

-Using this it is easy to identify where to add functions and its related data and which function is used to modify data.
-Features of oops :

[1] Object :-Objects is real world entity that is identify by its unique name.Object consist properties(attributes) and behaviours(methods).

For example a car is a object that have properties like colour, brand name etc. and behaviours like it can move forward and backward.

[2] class : Class is template from which object is created that contains variables for storing data and methods to performing operations on these data.Class have collection of similar type objects.

[3] Inheritance : Process of accessing the behaviours of base class by its derived class. Main advantage of inheritance is re-usability of the code.

(i) Single Inheritance : When a single base class is inherited by a single derived class then the inheritance is called as single inheritance.
For example parent child relationship.

(ii) Multilevel Inheritance : A base class inherited by derived and this derived class also inherited by its derived class.
For example grandfather's behaviours inherited by father and father's behaviours inherited by son.

(iii) Hierarchical Inheritance : A base class is inherited by more than one derived class.
For example sparrow, peacock, parrot inherits behaviours of bird class.

(iv) Hybrid Inheritance : Combination of all type of inheritance.

(v) multiple Inheritance : If a derived class inherits properties of more than one base class.
For example duck can swim and also fly.

-Ruby does not support multiple inheritance because it produce a conflict. For example if both the base class have same name then this will produce a confusion.

-So in ruby we can achieve multiple inheritance using module. Module provides a mixin facility. In mixin we can create modules that contains method and we can include more than one modules into our class and we can call modules method using object of class.

[4] Abstraction : Abstraction is the process of hiding implementation and showing only functionality to user.

For example Remote is a interface between user and TV.Using this we can on/off TV but we don't how to circuit works inside remote.
[5] Abstract class : Abstract class is class that has undefined "abstract" methods, which are left for subclasses to define. Abstract method does not have implementation.

For example each bike has a start methods but may be a different technique for different bike for start. Some bike starts by kick and some starts by pressing button.
So we can declare start method as a abstract method and we can implements it in derived class for new bike.

[6] Encapsulation : Encapsulation is a process of binding data and methods together.Encapsulation protect data from unwanted access or alteration by making it private.
Encapsulation = Abstraction + Data Hiding.

[7] Polymorphism : Different behaviours at different instances depend upon the data passed in the operation. Polymorphism increase code readability.

For example a software engineer performs many task such as sometimes he performs coding, sometimes he performs testing, sometimes he performs analysis. So software engineer is one but he is performing different task depending upon requirement.

There are two types of polymorphism.

(i) Compile time polymorphism : It is achieved by method overloading. In this, call to a overloaded method is decide at compile time.

(ii)Runtime Polymorphism : It is achieved by method overriding. In this, call to a method is decide at runtime.