Saturday, July 26, 2008

Five Main advantages of object-oriented programming

The popularity of Object Oriented Programming (OOP) was because of its methodology, which allowed breaking complex large software programs to simpler, smaller and manageable components. The costs of building large monolithic software were enormous. Moreover, the fundamental things in Object Oriented Programming are objects, which model real world objects. The following are the basic advantages of object-oriented systems.
Modular Design: The software built around OOP are modular, because they are built on objects and we know objects are entity in themselves, whose internal working is hidden from other objects and is decoupled from rest of the program.
Simple approach: The objects, we know, model real world, which results in simple program structure.
Modifiable: Because of its inherent properties of data abstraction and encapsulation the internal working of objects is hidden from other objects, thus any modification made to them should not affect rest of the system.
Extensible: The extension to the existing program for its adaptation to new environment can be done by simple adding few new objects or by adding new features in old classes/types.
Flexible: Software built on Object Oriented Programming, can be flexible in adapting to different situations because interaction between objects does not affect the internal working of objects.
Reusable & Maintainable: Objects once made can be reused in more than one program. Objects are separate entities, which can be maintained separately allowing fixing of bugs or any other change easily.
Class: Objects with similar properties are put together in a class. A class is a pattern, template, or blueprint for a category of structurally identical items (objects). OOPS programmers view Objects as instances of Class. A class is a blueprint from which objects can be created/ instantiated.
Class contains basic framework i.e. it describes internal organisation and defines external interface of an Object. When we say a class defines basic framework, we mean that it contains necessary functionality for a particular problem domain. For example, suppose we are developing a program for calculator, in which we have a class called calculator, which will contain all the basic functions that exists in a real world calculator, like add, subtract, multiply etc., the calculator class will, thus, define the internal working of calculator and provides an interface through which we can use this class. For using this calculator class, we need to instantiate it, i.e. we will create an object of calculator class. Thus, calculator class will provide a blueprint for building objects. An object which is an instance of a class is an entity in itself with its own data members and data functions. Objects belonging to same set of class shares methods/functions of the class but they have their own separate data members.
Defining the class does not create any objects, just as the mere existence of a type int does not create variables of type int.
A class is a description of a number of similar objects. A class has meaning only when it is instantiated. For example, we can use a class employee directly. We define a class employee and instantiate it.
Class Employee;
Employee John;
Now we can have various operations on John like compiite_salary of John.
Inheritance: Inheritance is the OOPS feature which allows derivation of the new objects from the existing ones. It allows the creation of new class, called the derived class, from the existing classes called as base class.
The concept of inheritance allows the features of base class to be accessed by the derived classes, which in turn have their new features in addition to the old base class features. The original base class is also called the parent or super class and the derived class is also called as sub-class.
An example:
Cars, mopeds, trucks have certain features in common i.e. they all have wheels, engines, headlights etc. They can be grouped under one base class called automobiles. Apart from these common features they have certain distinct features which are not common like mopeds have two wheels and cars have four wheels, also cars uses petrol and trucks run on diesel.
Abstraction: Suppose, for example, that you have a group of functions that can act on a specific data structure. To make those functions easier to use by, as far as possible, you can take the data structure out of the interface of the entity/ object, by supplying a few additional functions to manage the data. Thus, all the work of manipulating the data structure viz. allocating data, initializing, output of information, modifying values, keeping it up to date etc. can be done through the functions. All the user does; is to call the functions and pass the structure to them.
With these changes, the structure has become an opaque token that other programmers never need to look inside. They can concentrate on what the functions do, not how the data is organized. You have taken the first step toward creating an object.
Information hiding: The process of hiding details of an object or function is information hiding. Information hiding is a powerful programming technique because it reduces complexity. One of the chief mechanisms for hiding information is encapsulation — combining elements to create a larger entity. The programmer can then focus on the new object without worrying about the hidden derails. In a sense, the entire hierarchy of programming languages — from machine languages to high-level languages — can be seen as a form of information hiding.
Information hiding is also used to prevent programmers from changing — intentionally or unintentionally — parts of a program.
Encapsulation: In an Object oriented language, a class is clearly encapsulated as the data variables and the related operations on data are placed together in a class.
For example, in a Windows based software, the window object contains Window's dimensions, position, colour etc. Encapsulated with these data are the functions which can be performed on Window i.e. moving, resizing of Window etc. The other part of this window program will call upon window object to carry out the necessary function. The calling or interacting with the window object will be performed by sending messages to it. The required action will be performed by the window object according to its internal structure. This internal working is hidden from the external world or from the other part of the software program.

No comments: