C/C++ Projects :: Blog :: Features of Object Oriented Programming (OOP)


May 23, 2008

Object oriented programming is defined as a method of implementation in which programs are organized as cooperative collection of objects, each of which is an instance of some class.  Object based programming languages support the following features :

  • Data Abstraction
  • Data Encapsulation
  • Inheritance
  • Polymorphism

Now we will explain each one of them with examples :

Data Abstraction : "Abstraction refers to the act of representing the essential features without including the background details or explanations." It is the enforcement of a clear separation between the abstract properties of a data type and the concrete details of its implementation. The abstract properties are those that are visible to client code that makes use of the data type--the interface to the data type--while the concrete implementation is kept entirely private, and indeed can change, for example to incorporate efficiency improvements over time. The idea is that such changes are not supposed to have any impact on client code, since they involve no difference in the abstract behaviour.

For example, one could define an abstract data type called lookup table, where keys are uniquely associated with values, and values may be retrieved by specifying their corresponding keys. Such a lookup table may be implemented in various ways: as a hash table, a binary search tree, or even a simple linear list. As far as client code is concerned, the abstract properties of the type are the same in each case.

Data Encapsulation : "Data encapsulation, sometimes referred to as data hiding, is the mechanism whereby the implementation details of a class are kept hidden from the user." The user can only perform a restricted set of operations on the hidden members of the class by executing special functions commonly called methods. The actions performed by the methods are determined by the designer of the class, who must be careful not to make the methods either overly flexible or too restrictive. This idea of hiding the details away from the user and providing a restricted, clearly defined interface is the underlying theme behind the concept of an abstract data type.

For example, to create a stack class which can contain integers, the designer may choose to implement it with an array, which is hidden from the user of the class. The designer then writes the push() and pop() methods which puts integers into the array and removes them from the array respectively. These methods are made accessible to the user. Should an attempt be made by the user to access the array directly, a compile time error will result. Now, should the designer decide to change the stack's implementation to a linked list, the array can simply be replaced with a linked list and the push() and pop() methods rewritten so that they manipulate the linked list instead of the array. The code which the user has written to manipulate the stack is still valid because it was not given direct access to the array to begin with.

The concept of data encapsulation is supported in C++ through the use of the public, protected and private keywords which are placed in the declaration of the class. Anything in the class placed after the public keyword is accessible to all the users of the class; elements placed after the protected keyword are accessible only to the methods of the class or classes derived from that class; elements placed after the private keyword are accessible only to the methods of the class.

Inheritance : "It refers to a way to form new classes (instances of which are called objects) using classes that have already been defined." The new classes, known as derived classes, take over (or inherit) attributes and behavior of the pre-existing classes, which are referred to as base classes (or ancestor classes). It is intended to help reuse existing code with little or no modification.

For example, manager is a part of class employee. Here class manager acquires the characteristics of class employee. There are many types of inheritance, some of which are listed below.

  • Single Inheritance : When a class is derived from only one base class, it is called single inheritance.
  • Multiple Inheritance : Inheritance in which a derived class has several base classes.
  • Multilevel Inheritance : The method of deriving a class from another derived class is called multilevel inheritance.
  • Hierarchical Inheritance : When the traits of one class are inherited by more than one class.
  • Hybrid Inheritance : A method of inheritence where a class is derived from several derived classes.


Polymorphism : "Polymorphism is the process of using an operator or function in different ways for different set of inputs given." More precisely, polymorphism (object-oriented programming theory) is the ability of objects belonging to different types to respond to method calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, so this behavior can be implemented at run time (this is called late binding or dynamic binding).

It means that if class B inherits from class A, it doesn’t have to inherit everything about class A; it can do some of the things that class A does differently.means that if class B inherits from class A, it doesn’t have to inherit everything about class A; it can do some of the things that class A does differently.

 

Keywords: class, data abstraction, data encapsulation, inheritance, object oriented, polymorphism, programming

Posted by C/C++ Projects - Rahul


You must be logged in to post a comment.