Tuesday, 2 December 2014

OOPS Concept

-:Classes and Objects in OOPS:-

Classes are the most important constituents of Object Oriented Programming.

A class is just a template which contains the various attributes and functions of an object of the class.

For example,
Consider a class bird.
A bird class will have the following attributes:
1. Color
2. Height
3. Weight
4. Habitat
5. nature
It will also have the following functions
1. Flight
2. Sound
etc.,

A class gives you only the attributes.

If you ask 10 different persons to think of a bird and describe, each one will describe it differently. Because it is just a general name.

An object is a run-time entity of a class.
Say, you create an object parrot from the class bird. Then you know the value for all the parameters for this particular object. If you create another object duck, then this object will also have all the parameters defined for the class bird. But, the values for the parameters would be different from that of the object parrot.

 

-:Encapsulation:-

Encapsulation is the first pillar or principle of object-oriented programming. In simple words, “Encapsulation is a process of binding data members (variables, properties) and member functions (methods) into a single unit”. And Class is the best example of encapsulation.

"Wraping up data into single unit is know as a Encapsulation "

 

Need or purpose of encapsulation:-

  • To hide and prevent code (data) from the outside world (here the world means other classes and assemblies).
  • To prevent code (data) from accidental corruption due to programming errors so that we can deliver expected output. Due to programming mistakes, code may not behave properly and it has an effect on data and then it will affect the functionality of the system. With encapsulation we can make variables, properties, and methods private so it is not accessible to all but accessible through proper channels only to protect it from accidental corruption from other classes.

-:Polymorphism:-


Polymorphism means one name many forms. Polymorphism means one object behaving as multiple forms. One function behaves in different forms. In other words, "Many forms of a single object is called Polymorphism."

 

Real World Example of Polymorphism

Example 1

  • A Teacher behaves with student.
  • A Teacher behaves with his/her seniors.
Here teacher is an object but the attitude is different in different situations.

Example 2

  • Person behaves as a SON in house, at the same time that person behaves like an EMPLOYEE in the office.

Example 3

Your mobile phone, one name but many forms:
  • As phone
  • As camera
  • As mp3 player
  • As radio
With polymorphism, the same method or property can perform different actions depending on the run-time type of the instance that invokes it.
There are two types of polymorphism:
  1. Static or compile time polymorphism
  2. Dynamic or runtime polymorphism


  • Function Overloading:-  

                    Function name is same but argument or data type must be different in same class.


  • Function Overriding:-  

              Function name is same and argument or data type also must be same but they are in different class.


    -:Inheritance:-


    Inheritance is a mechanism of acquiring the features and behaviors of a class by another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. Inheritance implements the IS-A relationship.

    For example, mammal IS-A animal, dog IS-A mammal; Hence dog IS-A animal as well.

     

    Advantages:-

  1. Reduce code redundancy.
  2. Provides code reusability.
  3. Reduces source code size and improves code readability.
  4. Code is easy to manage and divided into parent and child classes.
  5. Supports code extensibility by overriding the base class functionality within child classes.

     Disadvantages

  1. In Inheritance base class and child classes are tightly coupled. Hence If you change the code of parent class, it will get affects to the all the child classes.
  2. In class hierarchy many data members remain unused and the memory allocated to them is not utilized. Hence affect performance of your program if you have not implemented inheritance correctly.

   -:Different Types of Inheritance:-

   OOPs supports the six types of inheritance as given below-

   1)Single inheritance:-

    In this inheritance, a derived class is created from a single base class.
 

2)Multi-level inheritance:-


In this inheritance, a derived class is created from another derived class.

3)Multiple inheritance:-


In this inheritance, a derived class is created from more than one base class. This inheritance is not supported by .NET Languages like C#, F# etc.
 

4)Multipath inheritance:-


In this inheritance, a derived class is created from another derived classes and the same base class of another derived classes. This inheritance is not supported by .NET Languages like C#, F# etc.
 

5)Hierarchical inheritance:-


In this inheritance, more than one derived classes are created from a single base.

6)Hybrid inheritance:-


This is combination of more than one inheritance. Hence, it may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple inheritance.
Since .NET Languages like C#, F# etc. does not support multiple and multipath inheritance. Hence hybrid inheritance with a combination of multiple or multipath inheritance is not supported by .NET Languages.

No comments:

Post a Comment