What is OOP? In Layman terms...

What is OOP?
 
OOP stands for Object Oriented Programming.
It is not just a programming language, but a paradigm (An example or model used to explain a concept or theory). OOP does not tell you how to program, rather it tells you how to go about designing your software. There are many languages that implement/help you in implementing OOP. C++ is one of them. When you are developing Object-oriented programs/software the emphasis is more on how you think about and design the software rather that on actually implementing(coding) it.
 
Why ‘Object-Oriented’?
 
      What should you do to make sure your program is object oriented? Simple, you stop thinking in terms of Bits, Bytes, Pointers, unions, structures, et al. Instead you think in terms of objects and the interactions between them. It is actually more natural for humans to think Object-oriented than to think in terms of memory and variables, etc.
       
      Simply put, OO is nothing but defining the behavior of software as a collection of objects and the interactions between them. 
 
What are Objects?
 
In the real world, an object is anything that is well-defined; say a car, a bat, a ball, a door, etc. An object has well-defined characteristics. These characteristics make one object (class of objects) easily differentiable from others. There is not much scope for confusion between, say, a bat and player.   
 
Let’s take an example. Say, you want to get your bike serviced. The steps you normally take are:-
  1. Go to the service station/garage.
  2. Fill out any request forms.
  3. Tell the mechanic about any specific problems, say horn not working. 
Can you identify the objects in this scenario? Try before reading further.
 
The objects involved are you, the garage, the forms, the bike and the mechanic.  
 
If you think about it, you’ll notice that you do not need to know the internal workings of the bike or the horn to get the problem rectified. We also don’t know what exactly the mechanic does to rectify the problem. We talk to the mechanic and the problem gets solved. As you surely see, each object has its functions, and objects can interact with each other. 
 
Let’s try to relate this to computers and programming. How would thinking in objects help in software development? 
 
OOP asks us to think about how a program can be reduced to objects and interactions between them. In OOP, an object is a collection of data and functions that operate on that data, which together represent a logical entity in the system.
 
The Procedural Programming model
 
C encourages what is called the procedural/modular programming model.
 
Some of the characteristics of this model are:-
  • System is organized around procedures.
  • Procedures send data to each other.
  • Procedures and data are clearly separated.
  • Focus on data structures, algorithms and sequencing of steps.
  • Procedures are often hard to reuse.
  • Lack of expressive and powerful visual modeling techniques.
  • This programming paradigm is essentially an abstraction of machine / assembly language.
  • Design models are a long step from implementation.
The main motivation in the procedural model is to develop code easily and quickly, with focus on code re-use through the use of modules/procedures.
 
The OO Programming model
 
OO programming places high importance on modeling problems. A program is successful if it solves the real life problems it models. Thus OO programs correspond closely to the real world problems they solve. They model the problem domain and users’ activities. This enables better communication and visualization for all stakeholders. Successful OO designs almost always begin with a visual “object model” of the problem domain, involving both the domain experts and software designers.
 
The problem with procedural programming is that while the domain experts talk about the problem domain in terms of the lingo, the software designers talk about structures and bytes. Many a time they are not able to convey meaning to each other. On the other hand OO enables both the domain experts and software designers to talk in terms of objects in the problem domain. Modeling improves the understanding of the designers. Would you let a contractor build your new house without blueprints?
 
The essence of modeling is to show all pertinent detail.
 
Some of the characteristics of this model are:-
  • System is organized around objects (not code).
  • Objects send messages (procedure calls) to each other.
  • Data and associated behavior are tied together in objects.
  • Modeling of the domain as objects so that the implementation naturally reflects the problem at hand.
  • Visual models are expressive and relatively easy to comprehend.
  • Focus on responsibilities & interfaces before implementation.
  • Visual models of the problem evolve into models of the solution.
  • Design models are only a small step from implementation.
This modeling makes it easier to build systems that solve the right problem, work properly, are maintainable, are extensible and are reusable.
 
OO programming concepts
    OO programming introduces a number of powerful concepts that make it very different from the pure modular approach. It is very important to understand and internalize these concepts before starting the software design and implementation.
 
The major OO concepts are:-
  • Objects
  • Classes
  • Encapsulation
  • Abstraction and Generalization.
  • Inheritance
  • Polymorphism
Objects
 
Objects represent real or abstract things, with a name. They are usually the actors/players in the problem. Going back to the original Bike example, each well-defined entity in that example is an object and may need to be modeled to implement a proper software solution.
 
Objects:-
  • Have well-defined responsibilities.
  • Exhibit well-defined behavior.
  • Have a well-defined interface, which is as simple as possible.
  • Are self-consistent, coherent, and complete.
  • Are (usually) not very complex or large.
  • Have knowledge of themselves and the interfaces of a small number of other objects.
  • Are as loosely coupled with other objects as possible.
By loosely coupled we mean that one object does not need to know the internal details of another object for the interaction between them to be successful. The interface that the object exposes to the outside world is very important. Since the internals of the object are private to itself, the interface should be well documented, so that others may (re)use them.
 
Drawing from C, objects can be thought of as being similar to variables of one type. Say we declare int a,b,c; Here a, b, and c are all variables(objects) of type int. They share the same characteristics, only their actual values are different.
Classes
 
Objects are instances of classes. A class is the template that defines the characteristics of a certain class of object. A class defines both the interface(s) and the implementation for a set of objects, which determines their behavior. This is very similar to structures in C; the major difference being that a class has both data members and functions related to them clubbed together. An object, once instantiated, cannot change its class.
 
E.g. once created, an int variable has the same amount of memory associated with it. The only thing that can change is its contents/value.
 
Class ClassName
{
public:
    // members
 
protected:
    // members
 
private:
    // members
};
 
Encapsulation
 
The term Encapsulation basically means that only the necessary interface is exposed to the outside world. The client of an object does not need to know the internal details of the object that it is interacting with.
 
Thus encapsulation deals with:-
  • Hiding the “gears and levers”.
  • Exposing only the client’s view of the object’s responsibilities.
  • Protecting the object from outside interference.
  • Protects other objects from relying on details that are likely to change.
  • Information hiding promotes loose coupling between objects and modularity, which promotes flexible design, which promotes reusability.
  • Reduces interdependencies in the code.
E.g. To drive a car successfully, you do not need to know how an Internal Combustion engine works.
 
class Person
{
private:
    int age;
 
public:
    int getAge() { return age; }
};
Here you can see that the internals are hidden/private and the interface is public.
 
Abstraction and Generalization
 
The term abstraction means that we try to generalize problems and their solutions. Generalization is a very powerful tool. The first step is to ignore complex details and simplify the problem.
 
Humans tend to use generalizations naturally. Whether we see a German shepherd or a Pomeranian, we think of both as “dogs”.
This is generalization. By thinking abstract or generalized, we make the problem easier to solve. We concentrate on the bigger picture, before getting into the details and nitty-gritty. Once we have a broad understanding, we dig deeper and try to get more and more details.

Inheritance
 
Inheritance is a way of describing a class by saying how it differs from another class.
 
E.g. “Class Y is like Class X except for the following differences… ”   
 
The Base class is the general (abstract) class and defines generic characteristics of a class of objects.
The child (derived) classes are more specific instances of the base class and add specific characteristics.
 
As an example :-
Referring to the examples above, we can get the following things:-
  • The Derived class inherits from the Base class; the Derived class extends the Base class; the Derived class is a specialization of the Base class.
  • The Derived class may provide additional state (member data), or additional behavior (member functions/methods), or it may override the implementation of inherited methods.
  • The Base class is a generalization of all its Derived classes; one could say: in general, all Pets have names.
  • Base class = parent class = superclass.
  • Derived class = child class = subclass.
C++ allows multiple inheritance, Java does not allow that.
 
Polymorphism
 
Polymorphism (many-forms) comes due to inheritance. This is possible because all child classes essentially have an IS-A relationship with the pase class. I.e. We can say, a Dog is a Pet. So a Dog can do all the things that a Pet can do.  Polymorphism is also known as dynamic binding. Since a child class “IS-A” base class, a pointer of the base class type can hold an object of the type child class. E.g. A Pet* can point to a Dog Object.
 
I.e. Pet* ptr = new Dog(); is valid.
 
In the case of C, static binding occurs. The complier will decide the bindings of function calls to the actual functions at compile time itself. In the case of C++, we have this concept called dynamic binding. As a Pointer of type Base Class can hold an object of type Child class, the compiler cannot decide the bindings at compile time. The decision is deferred, and at runtime C++ detects the type of object that is referred to by the pointer and the call is made.
 
E.g.
class Shape
{
    // members
    virtual void Draw()=0; // Shape is abstract, so it cannot be actually drawn.
};
 
class Circle: public Shape
{
    // members
        void Draw()
        {
            // Code to Draw a circle.
    }
};
 
class Square: public Shape
{
    // members
        void Draw()
        {
            // Code to Draw a square.
    }
};
 
Shape* ptr = new Circle(); // is valid.
ptr->Draw(); // Will use Circle::Draw() to draw a circle.
 
ptr = new Square(); // is valid.
ptr->Draw(); // Will use Square::Draw() to draw a square.
 
At compile time ptr->Draw() cannot be resolved, so the compiler defers the resolution to run-time.
This is simple words in dynamic binding, and is the way polymorphism is implemented in C++.
 
That's it for now. The first installment of What is OOP???
I may actually add details if time permits ;-) 

Comments

Chetan said…
Hey Manoj,

Thanks for this blog. It is really helpful for a newbie like me. I hope you write more such articles.

Chetan
Nehu said…
A very nice explanation of OOPS concepts by real world examples..

really impressive !!
Anonymous said…
A very nice explanation of OOPS concepts by real world examples..

really impressive !!
Chandrasekar said…
Awesome article, really more worth and more beneficial.

Thanks a lot!!!!!
Manoj Pillai said…
Thanks everyone for the praise.