Computing

Oops… I did it Again!

This article is going to explain the object-oriented programming (commonly called OOPS) concepts used in languages like C++ and Java. 

OOP's concepts have simplified programming like never before. Basically one can write programs in two ways: 

One way is the process-oriented model, where the programs are written in steps of code. C language uses this approach, and as the programs become lengthy, the code gets more complex. 

Other way is the object-oriented programming (OOP) way. OOP organizes a program around its data (also called objects) and a set of well-defined interfaces to that data. An object-oriented program can be called as a data controlling access to code. 

Abstraction is an essential element for this, which manages the complexity. In a sense, when someone works on a computer, not necessary that he should know the working of each and every part of the computer. Even without the hardware knowledge, he can e-mail, type or do other jobs on the computer. Thus people do not think of a computer as a unit made up of hundreds of cards and chips, but as a well-defined object with its own unique behavior. This is the advantage of abstraction.

Object-oriented programming is modeled on how, in the real world, objects are often made up of many kinds of smaller objects. This capability of combining objects, however, is only one very general aspect of object-oriented programming.

The Three Oops Concepts: 

1) Encapsulation:
It is the mechanism that binds together code and data in manipulates, and keeps both safe from outside interference and misuse. In short it isolates a particular code and data from all other codes and data. A well-defined interface controls the access to that particular code and data. In Java, the basis of encapsulation is the class. A class defines the structure and behavior (data and code) that will be shared by a set of objects. Each object of a given class contains the structure and behavior defined by the class, as if it were stamped out of a mold in the shape of a class. A class is a logical construct, an object has physical reality. When you create a class, you will specify the code and data that will constitute that class. Collectively, these elements are called the members of the class. Specifically, the data defined by the class are referred to as member variables or instance variables. The code that operates on that data is referred to as member methods or just methods, which define the use of the member variables.

Since the purpose of a class is to encapsulate complexity, there are mechanisms for hiding the complexity of the implementation inside the class. Each method or variable in a class may be marked public or private. The private methods and data can only be accessed by the code, that is a member of the class. The public method has all details essential for external users.

2) Inheritance:
It is the process by which one object acquires the properties of another object. This supports the hierarchical classification. Without the use of hierarchies, each object would need to define all its characteristics explicitly. However, by use of inheritance, an object need only define those qualities that make it unique within its class. It can inherit its general attributes from its parent. A new sub-class inherits all of the attributes of all of its ancestors.

3) Polymorphism:
It is a feature that allows one interface to be used for general class of actions. The specific action is determined by the exact nature of the situation. In general polymorphism means "one interface, multiple methods", 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 the compiler's job to select the specific action (that is, method) as it applies to each situation.

On the lighter side, extending this analogy to a dog, its sense of smell is polymorphic. If the dog smells a cat, it will bark and run around it. If the dog smells its food, it will salivate and run to its bowl. Note that the same sense of smell is at work in both the cases. The difference is what is being smelled, that is, the type of data being operated upon by the dog's nose!     

Next: Objects and Classes   

09-Apr-2001

More by :  Deepak Chandrasekaran

Top | Computing

Views: 3481      Comments: 0





Name *

Email ID

Comment *
 
 Characters
Verification Code*

Can't read? Reload

Please fill the above code for verification.