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
(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 takes 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 the bank balance.
(viii) Each object contains 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 suffices to know the type of message accepted and the type of response returned by the objects.
The figure below shows two notations for representing an object in an object-oriented approach.
(i) Objects contain data, and code to manipulate that data, it can make the entire set of data and code of an object 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 many objects belonging to that class.
(iv) it associates each object 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 behave like the built-in types of a programming language.
(vii) The syntax used to create an object does not differ from 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 the 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.
(i) Polymorphism is another important OOP concept.
(ii) Polymorphism, a Greek term, means the ability to take over 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 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 the different numbers 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 though 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.
(ii) Dynamic binding (also called late binding) means that the code associated with a given procedure call is not known until the call at runtime.
(iii) It is associated with polymorphism and inheritance.
(iv) A function call associated with a polymorphic reference depends on the dynamic type of that reference.
(v) Consider the procedure “Draw” in the figure above. By inheritance, every object will have this procedure. Its algorithm is, however, unique to each object and so the Draw procedure will be redefined in each class that defines the object. +At runtime, the code matching the object under the current reference will be called.
9. Message passing:
(i) An object-oriented program consists of a set of objects that communicate with each other. The process of programming in an object-oriented language, therefore, involves the following basic steps :
(a) Creating classes that define objects and their behavior,
(b) Creating objects from the class definition, and
(c) Establishing communication among objects.
(ii) Objects communicate with one another by sending and receiving information much the same way people pass messages to one another. The concept of message passing makes it easier to talk about building systems that directly model or simulate their real-world counterparts.
(iii) A message for an object is a request for execution of a procedure and therefore will invoke a function (procedure) in the receiving object that generates the desired result.
(iv) Message passing involves specifying the name of the object, the name of the function (message), and the information to be sent.
(v) Objects have a life cycle. They can be created or destroyed. Communication with an object is feasible as long as it is alive.