My Adsense

Monday, November 29, 2010

Introducing Object Oriented Concept

    In this section we will learn about 'How to think in Object Oriented way'.




    After completing this unit we should be able to:


* Think the real world in object oriented way

* Understand what really object and class means

* Describe what are attributes and responsibilities of an object.





In this unit, we will learn about the following topics:

* What's wrong with starting of Object Oriented Programming

* Philosophy of OOP

* Mapping Object Oriented Thoughts to Object Oriented Programming


What's wrong with starting of Object Oriented Programming

Unfortunately most of us start Object Oriented Programming (OOP) jumping into a language book like C++, Java or C# etc. But it's not a good start at all. Starting with OOP language book, we may learn writing package/namespace, class, object, interface, etc. But writing class, interface, object etc. does not guarantee good software design. OOP is totally useless if we can't use this concept for good software design. So, what should we know first? What should we know before starting with an OOP book?


First of all we should know: what's an object and how to think in OO (Object Oriented) way.


Philosophy of OOP

Yes, Starting of Object Oriented Programming is of course the concept of object.

And this concept is really simple. Ok, I am starting to speak about it. Before starting, I am requesting you to forget everything about programming, database design, software design i.e. all kinds of software engineering and programming language details for some couple of minutes (not forever!!).


Everything can be an object

Look around you, what we see, ALL ARE OBJECTS. We see computer, chair, table, pen, air cooler, carpet etc, all these are objects. Why they are object? Pretty simple, they have some related attributes and some related activities. How? Ok, just think about computer, it has processor, RAM, hard drive, keyboard, mouse, etc. All these are the attributes of computer. Now, what're the activities of a computer? It has a lot: it runs antivirus program, displays information, accepts requests and executes these properly. All these are the activities of computer. So, computer is an object. In the same way, we will get the attributes and activities of chair, table, etc. So, to be an object, something should have attributes and/or should have activities. Finally, from the above discussion, we can draw a conclusion:


Everything can be an object.


How it is possible? Is it possible that any event, like war or raining, singing, thinking etc (which is not visible) can be object? The answer is YES. These can be objects also. Think, raining has attributes: is it drizzling or "cats and dogs"? Singing can be an object if we consider, in which frequency-range it is sung?


'Name' – is it an attribute or an object? Actually, it is totally depend on problem domain. See, if you are thinking about a banking solution then customer 'Name' will be an attribute of customer. But if it a business from where you can purchase the 'Name' of your new-born baby then 'Name' will be an object to this business. Because 'Name' has meaning, length, type etc.


Note: In some OOP books, authors may use data instead attribute and responsibility (I personally think it is better term) instead of activity.


Object's data and responsibility depend on problem domain

If I tell you and your friend to find out the data and activity of customer object, suppose, you get customer with name, email, phone attributes and purchase, bargain activities. At the same time your friend may get customer with first name, middle name, last name, gender, credit card attributes and open account activity.


Why it happens? It happens because you and your friend were on different platform with different point of view. You think about the customer of a grocery store whereas your friend thinks about the customer of a bank. But both of you are right.


So, object and its data, responsibility everything totally depends on problem domain.


What is class?

Class is the blue print of object. Suppose, I say Johnny, Mary and Russell are students. Here, Johnny, Mary and Russell are objects and Student is the class. More specifically, Student is the class with name attributes and there are three student objects whose names are Johnny, Mary and Russell.





Walkthrough 1: Mapping Object Oriented Thoughts to Object Oriented Programming


To make our conception more clear, now we will try to make a small application. Of course, we will try it Object Oriented way.


Let's start. Suppose our considering problem domain as follows:

"………User wants to get full name and reverse full name providing first name, middle name and last name of a person."


We will identify the objects first. Here, we will get 'Person' object as follows:


    Has first name

    Has middle name

    Has last name


    Can tell its full name

    Can tell its reverse name


Now we will implement this in C# (VS 2008, DotNet 3.5), an OOP language.



  1. Start Microsoft Visual Studio 2008
  2. Click File>New>Project


we will see the following window. Select 'Class Library' template, give a meaningful name and select your preferable location.




  1. Click 'Ok' Button
  2. In Solution Explorer, we will see Class1.cs file. Delete this file.
  3. Select project (not solution) OOPWalkThrough1 and Click right button. Then Click on Add>Class from the popup menu. We will get following window.




  1. Remind we have found an object, 'Person' in our problem domain. Here we will give 'Person.cs' in Name as a file name.
  2. Click 'Add' button and we will see the following code


    using System;


    namespace OOPWalkThrough1







    Here, Person is a class.


  3. Now update the Person class as follows:


namespace OOPWalkThrough1




string firstName;

string middleName;

string lastName;


string GetMyFullName()


return firstName + " " + middleName + " " + lastName;



string GetMyReverseName()


string reverseName = "";

string fullName = GetMyFullName();

for (int index = fullName.Length - 1; index >= 0; index--)


reverseName += fullName[index];


return reverseName;





Have a look inside the Person class. Compare the identified object (Person) with the Person class.




We see data of Person are represented by attributes and responsibilities are represented by methods. So any object creates from Person class will represent our domain object, Person.


  1. Now add another class in this project. Here file name will be EntryPoint.cs (or any meaningful name you like) and write Main() method inside this class as follows:




Here, we will create a Person object, personObj from Person class using the following line:


        Person personObj = new


    Then assign some data to the attributes of personObj as follows:


personObj.firstName = "James";

personObj.middleName = "Lucas";

    personObj.lastName = "Scott";


    Finally, ask personObj to tell it's full name and reversed-name and get it:


string fullName = personObj.GetMyFullName();

string reverseName = personObj.GetMyReverseName();


  1. To make the project executable, we need to change the project properties. Select the project, click right button, select Properties from pop-up menu; we will get the following tab to change project properties.





Select Output Type as Console Application and Startup object as OOPWalkThrough1.EntryPoint.


  1. Run the application and we will the output:




  1. To take input from user we need to change the Main() method. Replace these three lines:


personObj.firstName = "James";

personObj.middleName = "Lucas";

    personObj.lastName = "Scott";


        with the following three lines


personObj.firstName = Console.ReadLine();

personObj.middleName = Console.ReadLine();

personObj.lastName = Console.ReadLine();



Important Note

Here, to keep the example simple, I've violated encapsulation (keep the data public) principle.

To solve a problem, mapping a domain object directly to software object sometimes may not be a good solution.






Walkthrough 2: Convert the application of Walkthrough 1 to Windows application.


  1. Open from the project, OOPWalkthrough1 from Examples & WalkThroughs\OOPWalkThrough1 location.
  2. Add a new Form class in your project (Right-click on project, click Add>Windows Form). In name textbox, type PersonInformationUI.cs and click Add button. Then you will see a windows form.
  3. Design the windows form as following UI and give the appropriate name of each control.




  1. Write the following code inside Show button_click event:


Person personObject = new

personObject.firstName = firstNameTextBox.Text;

personObject.middleName = middleNameTextBox.Text;

personObject.lastName = lastNameTextBox.Text;


fullNameTextBox.Text = personObject.GetMyFullName();

reverseNameTextBox.Text = personObject.GetMyReverseName();


  1. Change your Main() method of EntryPoint class with following code:


PersonInformationUI personInformationUIObj =




  1. Right-Click on project of solution explorer. You will see a popup and click properties. Select Application Tab, Change output type to Windows Application.
  2. Run the application and see it works.







Practice 1: Application for bank account transaction


User story:


"As a customer

I want to create an account and wants to do transaction (deposit and withdraw) on it."


See the UI prototyping:




When report button is clicked, information will be displayed as follows:




Run BankAccountOperation.exe from 3.1 Basic Concept\Practices for better understanding.