My Adsense

Saturday, March 21, 2009

Object Oriented Programming (Part 1)

OOP : The Language of GOD

Friends, now a days we see most of the programming languages are object oriented or the procedural lang. are getting converted into object oriented one by one.

Today’s lecture covers on the basics on OOP and the clear view of Class and Objects.

Before begin, let me know what do u think about the OOP  with examples.

Well there are numerous answers that we have memorized with examples. But, maximum of us just crammed the definition of the terms of OOP and didn’t think deeply about it. 

Now, before jumping in deeply, let me describe you it in brief.


We all know, OOP means Object oriented programming. Which means the orientation between the objects and make the objects works together in our defined way in the virtual world. 

Defined way means the programming language. Examples of the programming languages are Fortran, Cobol, C, C++, Java, Python, Cold Fusion, C#, Visual Basic and a lot more.

These languages are of two types. One is Object oriented and the other is structural or procedural programming.

But what’s the difference between these two? Or, what’s the advantage of OOP?

 Why OOP?

Procedural programming focus on programs/procedures with the best algorithms. Here, all of the data are shared and all of the code are situated in a single file which is has a very big amount of code. 

But object oriented programming focus on the objects of the problem domain and analysis the attributes and functionalities of those objects.

 Structural programming has some characteristics.

  • ·         It is Top-Down design
  • ·         Limited control structure
  • ·         Limited scope of data structure
  • ·         All are in one file
  • ·         Huge line of code

 Besides, OOP languages has some advantages:

  • ·         Modern thinking
  • ·         OOP can be upgrade from small to large scale
  • ·         Easy to partition the work
  • ·         Reduces the software maintenance & development costs
  • ·         Change in user requirement or later development always been a major prob. OOP is the solution.


Object Oriented Thinking: 

Now can you tell me what is an object? Ya ya..everything besides us is object. We all know the definition of that.But whats the characteristics of the objects? Or how can we implement the objects in our programming languages?

Let me give you a problem domain.

“ …customers are allowed to have different types of bank accounts, deposit money, withdraw money and transfer money between accounts”

What do you emphasize if you are asked to implement it in program?

Let me guess: you make methods of deposit money, withdraw money and transfer money. Isn’t it?

Wrong…wrong..its totally a wrong way of thinking. Remember, you are in an era of object oriented programming. You have to find the objects from there. Can you find out the objects from this problem  domain?

Hmm..you are right..customers, money, account are the objects.

Make the objects in the problem domain mapped in the objects in the software. 


Every objects in our world has something and they do something.

The things objects posses or has are data and the things they do are methods in programming language.

From our previous problem domain, we can be clear on this aspect.

Check that, the object ‘Customer’ is a human being. That means he has the attributes of a human being. So, he must have a Name, Age, Address, Social ID etc. These are his data or attributes.

And he can do somekind of works. Some of them are, Walking, Watching , Eating etc. These are the behavior or these thing he can do and we represent these as methods in programming language.

Objects communicate between them with message passing. Data are passed through the method as a message.

Now just look around you and think about 10 objects. And please write down the object names and their data and workings.

 

Class Vs Objects:

Have you finised writing the object names?? If yes, please again look around you. You can see that there are many objects which are of the same kind. They has the same kind of attributes and they do the work in the same way. More precisely, they are identical in some means. Just their data are changed. We can classify those objects in a single group which has the common attribute names and the functionality they posses.

Suppose, if we think about the pen, we see that, they are of different colors, size, design but they all do the same work, that is writing. Here, the data of the Pen is different but the method is same.

So, we can say that, if we make a blueprint of pen, we make many pen from that blueprint which are of different data with same methods.

A Class is essentially like a blueprint, from which we can create objects. A class defines characteristics of an object, including properties that define the types of the data that the object can contain and methods that describe the behavior of the object. These characteristics determine how other objects can access and work with the data that is contained in the object.

 

An object is an instance of a class. If a class is like a blueprint, then an object is what is created from that blueprint. The class is the definition of an item; the object is the item. The blueprint for your house is like a class; the house that you live in is an object.

 Let me give you some examples to differentiate these two.

Example 1:

Duck class that has certain behaviors, such as walking, quacking, flying, and swimming—and specific properties, such as height, weight, and color.

The Duck class defines what a duck is and what it can do. A Duck object is a specific duck that has a specific weight, color, height, and behavioral characteristics. The duck that you feed is a duck object.

Example 2:

Suppose that a programmer must write a function that changes a customer’s address in a database. In a traditional approach, the programmer may write a ChangeAddress function that takes a database table and row as a parameter and changes the address information in that row. Or, the programmer may use the person’s name as a parameter, search the table for that name, and then change the address in the record. The disadvantage of this approach is that when you want to change the information about the person, you must know something about how that information is represented, in this case, in a specific table in a database.

An object-oriented approach is to define a class that represents customers and provides the ability to change addresses. The application that uses the Customer class is likely to manage multiple customer objects, each representing one customer. Each customer object contains information about the location of that customer’s record in the database, so when the application must change the address information, it can simply invoke the ChangeAddress action or method for that particular customer. The application tells the Customer object to change its address, in effect. 

So, we can differentiate between the class with object as:

Class:

  • ·         Visible in source code
  • ·         The code is not duplicated

Object:

  • ·         Own copy of data
  • ·         Active in running program
  • ·         Occupies memory
  • ·         Has the set of operations given in the class


Hope you all understand clearly. If you have further query, feel free to ask me.
My email address: foyzulkarim@gmail.com


No comments: