Object Oriented Programming

Object oriented programming is a style of programming where data and code are encapsulated into the same element, called an object. Object oriented programming started as a way to implement programs for simulation purposes (the Simula language was the first one to use OO techniques).

To start understanding how object oriented programming works, we first provide an overview of the main features encountered in most object oriented languages. These include the classic features of encapsulation, inheritance, and polymorphism.

Encapsulation

One of the basic tenets of object oriented programming is that details about how parts of a program work should be encapsulated from other parts of the program. Only a small interface should be provided for communication between one module and the rest of the program. This is not very different from writing libraries in C, but it applies to a much smaller part of the program: the class. In a class, a C++ programmer can determine what parts can be accessed by the external parts of the program, and what parts can only be read by the class or its derived classes. With this mechanism, it becomes much easier to define elements that are part of the implementation only and the ones that should be seen by everyone.

The main advantage of encapsulation is that the programmer can give more attention to the parts of the program that are widely visible, while choosing to hide some parts that might change. In this way, a program can be maintained with less trouble, since only a small part of its behavior can be accessed from outside its main module.

Combination of data and behavior: another advantage of encapsulation in OO programming is that data and code are kept at the same level. Therefore, when data is defined, we can also determine what operations can legally access the data. For example, when defining a class representing a employee, the programmer can decide how the data will be manipulated and who is allowed to do so. The inclusion of code and data in the same unit also helps to maintain the encapsulation of code, since the programmer can now decide what code can have access to the inner workings of the object.

Inheritance

One of the features of object oriented programming is that one can create classes that inherit behavior from other classes. In this way, a class can have the same behavior as its parent class, but with additional behaviors added. It is a method of code reuse that provide easy access to the behavior of parent classes by derived classes. Object oriented languages vary in how many parent classes a particular class can have. For example, C++ allow a single class to derive from multiple parent classes. Java and C# allow only one parent class.

Polymorphism

For classes that derive from others, code can be reused or redefined. A method that is part of a class can be redefined in a derived class, providing a more useful behavior. For this to happen, object oriented languages provide a polymorphism mechanism that allows the program to select the right method to be called at each time. For example, if a drawing class derives from a parent class called Shape, it can redefine the Paint method to perform specific actions. The language will, at run time, determine what version of the Paint method should be called based on the object in which the method was invoked. This provides great flexibility to OO programs.

Conclusion

Object oriented programming is a methodology that stresses the close relation between data and code. In this way, it allows the definition of classes of behaviors that operate on data. Classes in OO languages can be specialized by subclasses. Encapsulation, inheritance, and polymorphism are the basic mechanisms used by OO languages to provide this functionality.

Tags: programming, object, oriented, languages
Article created on 2010-01-03 13:24:27

Post a comment