Saturday, January 24, 2015

Getting Started with Object Oriented Design - The Core Concepts

Object Oriented Approach consist of three basis steps :
  • Object Oriented Analysis (understand) What to do?
  • Object Oriented Design (plan) How to do?
  • Object Oriented Programming (build) Do it!

We are here to focus on the Basic Terminologies involved in Object Oriented Design.

What is an Object?

An Object is something that has its own characteristics and behavior. Object can be visible or non-visible, can be physical or not.

What is class?

A class is a blueprint of an object. For example : A House , we create a class for the House which defines the basic information of the house and include various attributes and behavioral description of the House. When we create an object we assign the values to the blueprints in the class and create our own different house with different colors , no of houses etc. Like this we can create even thousand different object for a class.

ABSTRACTION : When we say Table. We did not refer to a wooden table or a steel table. We just mentioned a table. This is called Abstraction, when we focus in the essentials and ignore the irrelevant or unimportant detail. Abstraction means that we wont create 2 different classes for Me and You. We will create only one class Person and create 2 different objects of that class for me and you with different attributes.

ENCAPSULATION : Encapsulation is surrounding something , not just to keep the contents together but also to protect it. In OOD Encapsulation refers to taking the attributes and the behavior, bundling them together in a same class. We also Encapsulate to hide the information inside a class that we do not want some other object to directly modify. For example we do not want that someone should be able to change the balance of bank account without going through the functions deposit() or withdraw(). We hide the balance attribute and only show the deposit and withdraw behavior of the object. This is also referred to the idea of black boxing where we do not care about the inner working of the box, but we just want to use only some of the functionality of the box. For example a telephone , we do not want to know how the call is connected to the number we enter , we just want to enter the number and talk.

INHERITANCE : Inheritance is extending support from a previously build class to create a new one. For example we had talked about the class Person. Suppose now we need one more class called Student and Employee. When we go to write the class Student we notice that we are having the same information as it is in Person class plus some more detail like the registration number or the course. So what we can do is instead of writing the same information for the Student or the Employee we can inherit the person class in both the Student and Employee to have all the attributes and the behavior of Person in them instead of writing them again for each class.


POLYMORPHISM (many + forms) : Polymorphism is doing the right thing at the right time knowing that we can do many different things for the same expression but we do what is correct for that expression. For example, we have and abstract class for a Bank Account extending its functionality to more specific sub classes Saving account or Investment Account(Inheritance). Now suppose we want that we can not withdraw more than 20,000 from a savings account. So what we do is we create a new function in Savings Account class and add this functionality to it. But the Bank Account class also had the same named behavior as Withdraw which simply withdraws the money from the account. This behavior is inherited by the Savings Account class. Now the saving account class has two behaviors for withdraw , one is inherited from Bank Account and another is defined in the Saving class itself. This is the class we call Overriding because the behavior we defined in the Saving class will override the behavior inherited from the Bank Account class. Now we can create thousands of objects of bank accounts using any of the class (Bank Account , Savings Account, Investment Account) we can use the same behavior withdraw on the all of these objects all together and we know that polymorphism will make sure the correct behavior is done automatically for the Saving Account when a withdraw is done.