What are the features of Object Oriented Programming?
Answer: Object Oriented Programming
Ans. The basic concepts of object oriented programming are :
1. Objects 2. Classes 3. Data hiding 4. Data encapsulation 5. Data abstraction 6. Inheritance
7. Polymorphism 8. Dynamic binding 9. Message passing
1. Objects :
(i) Objects are the basic run time entities in an object oriented system.
(ii) They may represent a person, a place, a bank account, a table of data or any item that the program has to handle.
(iii) They may also represent user defined data such as vectors, time and lists.
(iv) Programming problem is analyzed in terms of objects and the nature of communication between them.
(v) Program objects should be chosen such that they match closely with real world objects.
(vi) Object take up space in the memory and have an associated address like a record in Pascal or a structure in C.
(vii) When a program is executed, the objects interact by sending messages to one another. For example, if “customer” and “account” are two objects in a program, then the customer object may send a message to the account object requesting for the bank balance.
(viii) Each object contain data, and code to manipulate the data.
(ix) Objects can interact without having to know details of each other’s data or code.
(x) It is sufficient to know the type of message accepted, and the type of response returned by the objects.
Figure below shows two notations for representing an object in an object oriented approach.
2. Classes :
(i) Objects contain data, and code to manipulate that data, the entire set of data and code of an object can be made a user defined data type with the help of a class.
(ii) In fact, objects are variable of the type class.
(iii) Once a class has been defined, we can create any number of objects belonging to that class.
(iv) Each object is associated with the data of the type class with which they are created.
(v) A class is thus a collection of objects of similar type.
For example, mango, apple and orange are members of the class Fruit.
(vi) Classes are user defined data types and behaves like the built in types of a programming language.
(vii) The syntax used to create an object is no different than the syntax used to create an integer object in C. If Fruit has been defined as a class, then the statement Fruit mango; will create an object mango belonging to the class Fruit.
3. Data hiding :
The insulation of the data from direct access by the program is called data hiding or information hiding.
4. Data encapsulation :
(i) The wrapping up of data and functions into a single unit (called class) is known as encapsulation.
(ii) Data encapsulation is the most striking feature of a class.
(iii) The data is not accessible to the outside world, and only those functions which are wrapped in the class can access it. These functions provide the interface between the object’s data and the program.
5. Data abstraction :
(i) Abstraction refers to the act of representing essential features without including the background details or implementations.
(ii) Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost, and functions to operate on these attributes.
(iii) They encapsulate all the essential properties of the objects that are to be created.
(iv) The attributes are sometimes called data members because they hold information. The functions that operate on these data are sometimes called methods or member functions.
(v) Since the classes use the concept of data abstraction, they are known as abstract data type (ADT).
6. Inheritance :
(i) Inheritance is the process by which objects of one class acquire the properties of objects of another class.
(ii) It supports the concept of hierarchical classification. For example, the bird ‘robin’ is a part of the class ‘flying bird’ which is again a part of the class ‘bird’.
(iii) The principle behind this sort of division is that each derived class shares common characteristics with the class from which it is derived as shown in figure below.
(iv) 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 the combined features of both the classes.
(v) Note that each sub‐class defines only those features that are unique to it. Without the use of classification, each class would have to explicitly include all of its features.
7. Polymorphism :
(i) Polymorphism is another important OOP concept.
(ii) Polymorphism, Greek term, means the ability to take more than one form.
(iii) An operation may exhibit different behaviors in different instances. The behavior depends upon the types of data used in the operation.
Example : Consider the operation of addition. For two numbers, the operation will generate a sum. If the operands are strings, then the operation would produce a third string by concatenation.
(iv) The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.
(v) Figure below shows that a single function name can be used to handle different number and different type of arguments. This is something similar to a particular word having several different meanings depending on the context. Using a single function name to perform different types of tasks is known as function overloading.
(vi) Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface. This means that a general class of operations may be accessed in the same manner even through specific actions associated with each operation may differ. Polymorphism is extensively used in implementing inheritance.
8. Dynamic binding :
(i) Binding refers to the linking of a procedure call to the code to be executed in response to the call.