Monday, March 5, 2007

What is an Object?

In continuation with earlier discussion, Let's try to explore "What is an Object?".


I am sure you will correctly answer it as "It's an entity with State & Behavior". In earlier discussion we have explored what is a class & how to use that class in actual programming with simple (although bit non-realistic) example of Car & CarDemo classes. If you have understood the usage of a class, then you have unknowingly dealt with object as well, How?

See, In CarDemo class when we said:

Car carOne=new Car();
Car carTwo=new Car();

Don't worry too much how these Java classes are compiled & executed.

Note for C Programmers: For the time being assume 'new' similar to 'malloc(...) ' function.

carOne & carTwo objects of type Car.carOne has some state(values of data members) & carTwo has some other State although behavior(methods) is common as they are of type Car.

Few important things to note from example are

1) We are changing State of any object by "Invoking methods" & do not change data members directly.In CarDemo class we have created object carOne, carTwo & then we have invoked method to set the name of a car , change a gear, accelerate, applying breaks which intern changes name, gear, speed etc. data member values which is revealed through output.

2) Hiding internal state of an object & allowing other objects to change the state through method interaction is known as Data Encapsulation(Very important aspect of Object Oriented Programming ....More on this later)Apart from state & behavior object has two additional characteristics namely Identity & Responsibility.

So intern any Object has four distinct Characteristics:
1) State
2) Behavior
3) Identity
4) Responsibility

We will understand this with same example 'Car'

State:

When we say Car has a certain state (travelling with certain speed or at rest), we are actually specifying current values of its attributes/data members (Speed, Fuel Level,Make, Type of Fuel, Tyre Pressure etc..), So In OOPs paradigm we will define State of an Object as "State of an object is values of its data member at any point of time".

State I hope this is clear as we have discussed this number of times & i don't want to hang you on the same point too long.


Behavior:

Object has an behavior which intern changes State of an Object.In our Car example when we talk about accelerate as a behavior of an object (we represent behavior as a method in a class) it increases value of 'speed' data member & reduces value of Fuel Level (for the sake of example... In our class we don't have this attribute). Behavior could be expressed as "How certain object acts/reacts in terms of its State change/operations performed on it"In summary , For a car object what are the possible operations we can perform & how car will react by means of its state change defines Behavior of a car.

Identity:

Term is self explanatory i guess, Identity is the attribute/property of an object which will uniquely distinguish it from all other similar objects. e.g. If there are 10 cars of same company, same model, same color are parked in front of a building how one could distinguish them?......Yes you are right by their Number Plates....So registration number/ some number given by state authority/whom sever is a property which distinguishes one car from other even if all other attributes are same.....right? Kindly don't assume that Identity has to be a single property in some cases it could be group of properties which will act as identity of an object. e.g. If i consider Bank object perhaps only account number can't act as identity we have to consider bank name as well, Since there is a high chance that there will be a bank account number '25000' existing in Bank A & Bank B.

Responsibility:

In our example of Car, we could design the entire Car class which will represent actual car object of real world.....right?...In that case we could have say that Car as a whole system is composed of several objects e.g. Engine, Windows,Doors,Fuel tank etc...Ok? so in this whole system each object has some responsibility so that Car as a Cohesive unit/system."Responsibility of an object is a role it performs in a overall system". e.g. Fuel Tank has a responsibility of holding fuel.This concludes discussion about an object for the time being, In next post we will explore the Buzzwords in OOPs & their actual meaning. Subsequently we will see what are common interview questions on this topic as we are in the final stages in the discussion on OOPs .Till then...Bye Bye...Stay tuned.............

No comments:

 
online degree programs
visit us .