Sunday, May 29, 2011

THE THREE OOP PRINCIPLES

All the object-oriented programming languages provides mechanisms that help you implement the object-oriented model. They are encapsulation, inheritance, and polymorphism. Lets discuss all these in detail.

Encapsulation

Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. One way to think about encapsulation is as a protective rapper that prevent the code and data from being arbitrarily accessed by other code defined outside the rapper. Access to the code and data inside the rapper is tightly controlled through a well defined interface. To relate this to this real world, consider the automatic transmission on an automobile. It encapsulate hundreds of bits of information about your engine, such as how much your are accelerating, the pitch of the surface you are on, and the position of the shift lever. You, as the user, have only one method of affecting this complex encapsulation. By moving the gear-shift lever you cant affect the transmission by using the turn signal or windshield wipers, for example thus, the gear shift lever is a well defined (Indeed, unique) interface to the transmission. Further, what occurs  inside the transmission doesn't affect  object outside the transmission. For example, shifting gear doesn't turn on the headlights! Because an automatic transmission is encapsulated, dozen of car manufacturers can implement one in any way they please. However, from the driver's point of view, they all work the same. This same idea can be applied to programming. The power of encapsulation is that everyone knows how to access it and thus can use it regardless of the implementation details-and without fear of unexpected side effects.


Inheritance

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

As mentioned earlier, most knowledge is made manageable by hierarchical (that is, top-down) classifications. For example, a Golden Retriever in part of the classification dog, which in turns in part of the mammal class, which is under the larger class animal.

Without the use of hierarchies, each object would need to define all of its characteristics explicitly. However by use of inheritance, an object need only define those qualities that makes it unique within it's class. It can inherit its general attributes from its parent. Thus it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case.


Inheritance interacts with encapsulation as well. If a given class encapsulates some attributes, then any subclass will have the same attributes plus any that it adds as part of its specialization.




This is a key concept which lets object-oriented programs grow in complexity linearly rather than geometrically. A new subclass inherits all of the attributes of all of its ancestors. It does not have unpredictable interactions with the majority of the rest of the code in the system.


Polymorphism

Polymorphism (From the Greek, meaning " many forms") is a feature that allows one interface to be used for a general class of actions. The specified action is determined by the exact nature of the situation. Consider a stack ( which is a last-in, first-out list).

You might have a program that requires three types of stack. One stack is used for integer values, one for floating-point values, and one for characters. The algorithm that implement each stack is the same, even though the data being stored is differs. In a non object-oriented language, you would be require to create three different set using different names. However, because of polymorphism, in Java you can specify a general set of stack routine that all share the same names.

More generally, the concept of polymorphism is often expressed by the phrase "one interface, multiple method". This means that 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 compiler's job to select the specific action (i.e., method) as it applies to each situation. You, as a programmer, do not need to make this selection manually. You need only to remember and utilize the general interface.

1 comment:

  1. Great informative post! We are India based manufacturers and suppliers of all type gears offers all over the at competitive rates.

    ReplyDelete