Wednesday 18 December 2013

Introduction to Object Oriented Programming Concepts (OOP)

Object-oriented programming (OOP) is a programming paradigm that represents concepts as “objects” that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs. OOP is a design philosophy. Everything in OOP is grouped as self sustainable "objects". Hence, you gain re-usability by means of four main object-oriented programming concepts.
  1. Data Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
The core of the pure object-oriented programming is to create an object, in code, that has certain properties and methods. While designing C++ modules, we try to see whole world in the form of objects. For example a car is an object which has certain properties such as color, number of doors, and the like. It also has certain methods such as accelerate, brake, and so on.

There are other principle concepts that form the foundation of object-oriented programming:
  1. Objects
  2. Class
  3. Dynamic Binding
  4. Message Passing

Object:

This is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object.
Objects are identified by its unique name. An object represents a particular instance of a class. There can be more than one instance of an object. Each instance of an object can hold its own relevant data.
An Object is a collection of data members and associated member functions also known as methods.

For example whenever a class name is created according to the class an object should be created without creating object can’t able to use class.

In pure OOP terms an object is an instance of a class.

Class:

When you define a class, you define a blueprint for an object. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
 Classed are data types based on which objects are created. Objects with similar properties and methods are grouped together to form a Class. Thus a Class represents a set of individual objects. Characteristics of an object are represented in a class as Properties. The actions that can be performed by objects become functions of the class and are referred to as Methods.
For example consider we have a Class of Cars under which Santro Xing, Alto and WaganR represents individual Objects. In this context each Car Object will have its own, Model, Year of Manufacture, Color, Top Speed, Engine Power etc., which form Properties of the Car class and the associated actions i.e., object functions like Start, Move, and Stop form the Methods of Car Class.
Note: No memory is allocated when a class is created. Memory is allocated only when an object is created, i.e., when an instance of a class is created.

The above template describe about object Student
Class is composed of three things name, attributes, and operations

public class student
{

}
student  oStudent = new student ();

According to the above sample we can say that Student object, named oStudent, has created out of the student class.


In real world you will often find many individual objects all of the same kind. As an example, there may be thousands of other bicycles in existence, all of the same make and model. Each bicycle has built from the same blueprint. In object-oriented terms, we say that the bicycle is an instance of the class of objects known as bicycles. In the software world, though you may not have realized it, you have already used classes. For example, the Textbox control, you always used, is made out of the Textbox class, which defines its appearance and capabilities. Each time you drag a Textbox control, you are actually creating a new instance of the Textbox class.

How to identify and design a Class?


This is an art; each designer uses different techniques to identify classes. However according to Object Oriented Design Principles, there are five principles that you must follow when design a class,
  • SRP - The Single Responsibility Principle -
    A class should have one, and only one, reason to change.
  • OCP - The Open Closed Principle -
    You should be able to extend a classes behavior, without modifying it.
  • LSP - The Liskov Substitution Principle-
    Derived classes must be substitutable for their base classes.
  • DIP - The Dependency Inversion Principle-
    Depend on abstractions, not on concretions.
  • ISP - The Interface Segregation Principle-
    Make fine grained interfaces that are client specific.

Abstraction:

Data abstraction refers to, providing only essential information to the outside word and hiding their background details, i.e., to represent the needed information in program without presenting the details.
Example1: A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, usb ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works.  You just need to know how to operate the laptop by switching it on. Think about if you would have to call to the engineer who knows all internal details of the laptop before operating it. This would have highly expensive as well as not easy to use everywhere by everyone.

So here the Laptop is an object that is designed to hide its complexity.

How to abstract: - By using Access Specifiers


Encapsulation:


The encapsulation is the inclusion within a program object of all the resources need for the object to function - basically, the methods and the data. In OOP the encapsulation is mainly achieved by creating classes, the classes expose public methods and properties. The class is kind of a container or capsule or a cell, which encapsulate the set of methods, attribute and properties to provide its indented functionalities to other classes. In that sense, encapsulation also allows a class to change its internal implementation without hurting the overall functioning of the system. That idea of encapsulation is to hide how a class does it but to allow requesting what to do.


In order to modularize / define the functionality of a one class, that class can uses functions/ properties exposed by another class in many different ways. According to Object Oriented Programming there are several techniques, classes can use to link with each other and they are named association, aggregation, and composition.
There are several other ways that an encapsulation can be used, as an example we can take the usage of an interface. The interface can be used to hide the information of an implemented class.
When using Data Encapsulation, data is not accessed directly; it is only accessible through the functions present inside the class. Data Encapsulation enables the important concept of data hiding possible.

Encapsulation can be called as the core concept of OOP because it is encapsulation that reduces the maintenance burden, and limiting your exposure to vulnerabilities. The programmer has to design a well-defined interface to control the access of a particular code and data. A class defines the data and code which is shared by a set of objects. A class is a logical construct and an object is a physical construct which exists in reality that is like a chair or a table. A class comprises of code and data which are collectively called members of the class.

Inheritance:

One of the most useful aspects of object-oriented programming is code re-usability. As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class.
This is a very important concept of object-oriented programming since this feature helps to reduce the code size.
It is the technique used in OOP that one object acquires the properties of another object without redefining in order to create well defined classes. This technique also supports the hierarchical classification in which each object would not need to be redefined by all its characteristics explicitly. It prevents the programmer from unnecessary work and it is the inheritance concept that does this job. Inheritance gives us the facility to an object to inherit only those qualities that make it unique within its class. In general, it can inherit any attributes from its parent. Again to be well noted that it is the inheritance that get both data and functionality. We can later add any new data and methods needed. Different names have been used in different times for a parent class and a child class. The concept of parent and child class was developed to manage generalization and specialization in OOP and it is represented by a is-a relationship. But now-a-days, the following terms in object oriented programming is commonly used names given to parent and child class.
  • Super Class: Parent Class
  • Sub Class: Child Class
  • Base Class: Parent Class
  • Derived Class: Child Class
See more in Inheritance blog.

Polymorphism:

The ability to use an operator or function in different ways in other words giving different meaning or functions to the operators or functions is called polymorphism. Poly refers to many. That is a single function or an operator functioning in many ways different upon the usage is called polymorphism.
In OOP the polymorphisms is achieved by using many different techniques named method overloading, operator overloading, and method overriding.
See more in Polymorphism blog.

Example:
  • Operator Overloading 
  • Function Overloading
  • Function Overriding

Dynamic Binding:

Refers to linking of function call with function definition is called binding and when it is take place at run time called dynamic binding.

Message Passing:

Message passing is nothing but sending and receiving of information by the objects same as people exchange information. So this helps in building systems that simulate real life. Following are the basic steps in message passing.
The process by which one object can interact with other object is called message passing.
  • Creating classes that define objects and its behavior.
  • Creating objects from class definitions
  • Establishing communication among objects
In OOPs, Message Passing involves specifying the name of objects, the name of the function, and the information to be sent.

No comments: