Search This Blog

Saturday, 15 October 2011

Object Oriented Programming Concepts

0 comments
Object Oriented Concepts
An object is an entity in a real-world problem. It may represent a person, a place, a bank account, a table of data or any other item in the real world. In software scenario, an object refers to a piece of software containing data and code that can be reused.

Video Presentation on Object Oriented Programming Concepts
An object in a running program takes up space in memory and has an associated address to refer to it. Examples for objects used in a banking application are: “customer” and “account.” During execution, objects interact with each other by sending messages and receiving responses.
Class & Object:
Class is a user-defined data type, which defines the structure of an object. Objects are variables or instances of type class. A class defines the type and scope of all its members.
There are two elements defined in a class. They are variables and functions. Variables declared in a class are called data members, and the functions defined in a class are known as member functions.
Every class describes possibly infinite set of individual objects; each object is said to be an instance of its class and each instance has its own value for each attribute. Following are the various definitions of ‘class’ data type:
  1. A class is a template that unites data and operations.
  2. A class is an abstraction of real world entities having similar properties.
  3. A class identifies a set of similar objects.
Defining a member function:
A member function is a function, which is declared and defined for a class. Generally, member functions are declared within the class and defined outside the class. The function definition consists of two sections: a header and a body. The header part defines the signature of the function. It includes return type, name of the function and a set of parameters (variables used for passing information) with their type defined.
The function body contains the code that implements the function. This part can be written either inside or outside the class. To define the function outside the class, use the class name and the scope resolution operator (::) as a prefix to the function name.
Three types of scope specifiers are available for defining the scope of the members in a class. They are: private, protected and public. The scope specifier defines the availability of a member to the outside world. When not specified, the default scope of the members is private.
The private members are available only within the class. Normally, the data members that are hidden from outside access are declared as private variables. But, the member functions are declared under public scope, because they are free to access from other parts of the program. As member functions are the only way to access the private data and functions provided in an object, they are known as ‘interfaces’ to the object.
Encapsulation & Data Hiding:
The wrapping up of data and functions into a single unit (called class) is known as encapsulation. Though a class contains both data and functions, only the functions are available to the public, but not the data. The encapsulated data within the class are not accessible from outside world directly.
The hidden data can be accessed only through the member functions, which act as interfaces between the data and the external program. By declaring variables as private members, we can hide the data of an object. Variables declared as protected also implement data hiding. Such an act of hiding the data from public access is also referred to as data abstraction.
Abstraction of Function definition:
Abstraction refers to the act of representing essential features without exposing the background details required for implementing them. Like data, functions are also abstracted form the user by not showing the details of implementation.
Abstraction of function definition is done by hiding the details like logic and statements used for implementing the services provided by the object. In order to avail the services exposed by an object, the user has to know only the way of accessing the function and not the procedure used to implement it.
Polymorphism:
Polymorphism means having one name but different forms. Polymorphism allows two member functions of a same class to have the same name, but different parameters or return type. There are two types of polymorphism: Compile-time polymorphism and Run-time polymorphism.
Function overloading is an example for compile-time polymorphism. Two functions are said to be overloaded when they have the same name but different number/type of parameters or return type. Because the selection of an overloaded function is determined at compile time by the compiler depending on the parameter passed during function call.
Run-time polymorphism occurs during the function call of an over-ridden function. When a function of a base class (a class used for defining another class) is redefined in its derived class (class newly created from an existing one), it can have the same name, the same set of parameters and the return type as that of the base class function, but different body. In such a case, the selection of the function (function of the base class or the derived class) to be executed will be determined at run-time by the compiler.
Inheritance:
If we want to model an object whose functionality is basically similar to that of another object, then the new object can derive its basic functionalities from an already existing object. For example, the object ‘plastic chair’ inherits all the basic qualities like arms, legs and seat from an idol chair in addition to the extra qualification “material used” for making it.
Inheritance is a concept of OOP that defines the mechanism for creating a new class from an existing class called base class. The base class can be added on or altered to create the new class called derived class. In this way, a hierarchy of related classes can be created and reused in an object oriented programming.

Leave a Reply