Thursday, February 18, 2016

OOPs Concepts


Abstaractions, Encapsulation, Inheritance, Polymorphism and Composition are the important concepts for the object oriented programming/designs.

Abstraction:

Abstraction focuses only on outside view of an object. Helps us to identify which information should be visible and which information should be hidden.

Encapsulation:

Its a mechanism to hide the data, internal structure, and implementation details of an object.

All interaction with the object is through a public interface of operations.

Encapsulation means any kind of hiding:

-- Data Hiding
-- Implementation of Hiding
-- Class Hiding (Behind abstarct class or interface)
-- Design Hiding
-- Instantiation hiding

       Data Hiding: Use private members and appropriate accessors and mutators when possible.

       For example:
      • Replace
                 public float accuracy;
      • With
               private float accuracy;
               public float getAccuracy ( ) {
                  return (accuracy);
              }
              public void setAccuracy (float acc) {
                  accuracy = acc;
             }

                   
Inheritance [IS-A Reletionship]:

Method of reuse in which new functionality is obtained by extending the implementation of an existing object.
Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another.
The class which inherits the properties of other is known as subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

Polymorphism:

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic. 

Composition [HAS-A Relationship]:

Method of reuse in which new functionality is obtained by creating an object composed of other objects.

When we say “one object is composed with another object” we mean that they are related by a HAS-A
relationship.


The new functionality is obtained by delegating functionality to one of the objects being composed.
 

Composition encapsulates several objects inside another one.


 

No comments: