Wednesday 23 February 2011

OOP Principles

All object-oriented programming laanguages provide mechanisms that help you implement the object-oriented model. They are encapsulation, inheritance, and polymorphism.

ENCAPSULATION:
Encapsulation is the emchanism that binds together the code and the data it manuplates, and keeps safe from outside interference and misuse. It is a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled through a well defined interface.

In java, the basis of encapsulation is Class. Since the purpose of a class is to encapsulate complexity. A class defines the structure and behaviour (code and data) that will be shaared by sset of objects . Each object of given class contains the structure and behaviour defined by the class. For this reason, objects are sometimes referred to as INSTANCES OF A CLASS. Thus, a class is a logical construct ; an object has physical reality.

INHERITANCE:
Inheritance is the process by which one object acquires the properties of another object. This is important because it supports the concept of hierarchical classification

There is one SUPERCLASS and one SUBCLASS in the process of inheritance.
Superclass is the main class or Base class from which another class or Child class is inherited. and Subclass is the derived class wwhich inherits all the properties if the main class.

POLYMORPHISM:
Ploymorphism ( from the reek, meaning "many forms") is a feature that allows one interface to be used for a general class of actions . The specific action is determined by the exact nature of the situation.
More generally. the concept of polymorphism is often expressed by the phrases, "one interface , multiple methods." This means it is possible to design a generic interface to a group of related activities. This helps reduce complexity by allowing the same interface to be used to specify a general class of action. It is complier's job to select the specific action as it applies to each situation.

Example:
Consider a stack ( which is last in first out list ). you might have a program that requires three types of stacks. one stack is used for integer values , one for floating point values, and one for characters. The algorithm that implement each stack is same, even though the data being stored differs. In a non object oriented language, you would be required to create three different sets of stack routines , with each set using different  names. However, because of polymorphism, in java you can specify a general set of stack routines that all share the same names. 

No comments:

Post a Comment