Wednesday, September 21, 2011

The Difference between Class and Object in OOP

When we talk about Object Oriented Programming, we often talk about Class and Object at first. Do you know what is their difference? If you search on Google or any source, you will find that Object is an instance of Class. Yeah, that's the difference, but do you know what is the meaning of that? I will give you a simple abstraction. Here is the explanation :

Imagine you're an architect and you want to build some house. If you're a real architect, you will create a design or blueprint for the house first. The blueprint defines the template for the house that you want to build. If the blueprint has 3 rooms, 2 bathrooms, 3 doors and 1 garage, the house that will be built will has same template with the blueprint. It will also has 3 rooms, 2 bathrooms, 3 doors and 1 garage. So all the houses that built with that blueprint will have exactly same template.

The concept above same with Class and Object in OOP. Class is like the blueprint and Object is like the real house. So you define the templates, attributes, or behaviour of an Object in their class. If you define a Home Class and define some attribute like totalDoor, totalBathroom, etc, the Object that instantiated from this class will has same attribute. You can create many object from one class. It's like the concept above that you can create many house with one blueprint.

Class = Blueprint
Object = Real House

The other analogy is a Person Class. You define name and age attribute and some behaviour as its method. You can create Object from that class like PersonOne with name = Andy and age = 19. You can create another object like Person Two with name = Lily and age = 18. The two object are separate Object but created from one Class. The PersonOne and PersonTwo have same attribute, but the values of the attributes is different between each object.

From this analogy, we can conclude that Object is a real form of a Class. Object is a representation of object in real world that will have an interaction or is necessary with your program.On the other side, Class is the blueprint of that object.

So the steps is :
  1. Make an abstraction of the real world object that is necessary in your program.
  2. Make a class that will be a blueprint for that object. Define the attributes, method (behaviour), etc.
  3. Make one or some object from 
Hopefully this post will make you understand about the difference between object and class. If you have more question, please let me know.. Regardss..!!! ^_^

No comments:

Post a Comment