Basics of OOP

Objects

Objects are basic runtime entities in an object-oriented system. They may represent a person, a place, a bank account, a table of data or any item that the program may handle. Any programming problem is analyzed in terms of objects and the nature of communication between them. Objects are chosen such that they match closely with the real-world objects. An object takes up space in the memory and has an associated address. When a program is executed, the objects interact by sending messages to one another. For example, in a banking program, 'customer' and 'account' are two objects. The customer object may send a request to the account object requesting for the balance. Each object contains data and code to manipulate that data. The basic approach of object-oriented programming is that objects can interact without having to know the details of each other's data or code.

Classes

A class may be thought of as a data type and an object as a variable of that data type. A class is essentially a description of how to make an object that contains fields and methods. It provides a sort of template for an object. Classes are user-defined data types and behave like the built-in types such as int. Each object is associated with the data of type class with which they are created. Therefore, we can say a class is a collection of objects of similar type. For example, mango, apple and orange are members of the class fruit.

Data Abstraction and Encapsulation

The wrapping up of data and methods into a single unit (i.e. class) is called as encapsulation. Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world and only those methods, which are wrapped in the class, can access it. These methods provide an interface between the object's data and the program. This insulation of the data from direct access by the program is called data hiding. Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes and the methods that operate on these attributes.

Inheritance

Inheritance is the process by which objects of one class acquire the properties of objects of another class. For example, Truck is a part of the class HeavyVehicles, which in turn is again a part of class Vehicles. In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible by deriving a new class from the existing one. The new class will have all the features of class from which it is derived and its own features as well. In Java, the derived class is known as 'subclass'.

Polymorphism

Polymorphism is an important concept of OOP. Polymorphism means ability to take more than one form. This means that an operation may exhibit different behavior in different instances. The behavior depends upon the types of data used in the operation. For example, consider the operation of addition. If operands are two numbers, the operation will result in the third number, which is the sum of these two numbers. But if the operands are two strings, then the operation would produce a third string by concatenation. Polymorphism is somewhat like a single word, having several different meanings depending on the context. Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface.

Dynamic Binding

Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic Binding means the code associated with a given procedure call is not known until the time of the call at runtime. Dynamic binding is associated with polymorphism and inheritance.

Message Communication

An object-oriented program consists of a set of objects that communicate with each other. The process of programming in an object-oriented language, involves the following basic steps:

Creating classes that define objects and their behavior.

Creating objects from class definitions.

Establishing communication among objects.

Objects communicate with one another by sending and receiving information just the same way as people pass messages to one another. A message for an object is a request for execution of a procedure, and therefore will invoke a method in the receiving object that generates the desired results.

Benefits of OOP

Through inheritance we can eliminate redundant code and extend the use of existing classes.

We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch.

Data hiding helps the programmer to build secure programs that cannot be invaded by code in other parts of the program.

It is easy to partition the work in a project based on objects.

The data-centered design approach enables us to capture details of a model in implementable form.

Object-oriented systems can be easily upgraded from small to large systems.