Object-Oriented Programming

Object-Oriented Programming
Object-Oriented Programming

Basic concept:


Object-oriented programming is centered around objects that encapsulate both data and the functions that operate on them.

Explanation:
Object-oriented programming OOP is centered on creating objects. An object is a software entity that combines both data  and procedures in a single unit. An object’s data items, also referred to as its attributes, are stored  in  member  variables.  The procedures that an object performs are called its member functions. This bundling of an object’s data and procedures together is called encapsulation.

NOTE
In some object-oriented programming languages, the procedures that an object performs are called methods.

Below the Figure  shows a representation of what a Circle object we create might look like. It has just
one member variable to hold data and two member functions. The Circle object’s data item is its radius. Its first function sets the radius, and its second function calculates and returns the area.
                                   
The member variable and the member functions are all members of the Circle object. They are bound together in a single unit. When an operation is to be performed, such as calculating the area of the circle, a message is passed to the object telling it to perform the getArea function. Because getArea is a member of the Circle object, it automatically has access to the object’s member variables. Therefore, there is no need
to pass radius to the getArea function.

Not only do objects encapsulate associated data and functions, they also permit data hiding.Data hiding refers to an object’s ability to hide its data from code outside the object. Only the object’s member functions can directly access and make changes to the object’s data. An object typically hides its data, but allows outside code to access it through some of its member functions. As the second Figure illustrates, the object’s member functions provide programming statements outside the object with a way to indirectly access the object’s data.


When an object’s internal data is hidden from outside code, and access to that data is restricted to the object’s member functions, the data is protected from accidental corruption. In addition, the programming code outside the object does not need to know about the format or internal structure of the object’s data. The code only needs to interact with the object’s functions. When a programmer changes the structure of an object’s internal data, the object’s member functions are also modified so they will still properly operate on
the data. These changes, however, are hidden from code outside the object. The way in which outside code interacts with the member functions does not change.

Previous
Next Post »