MY mENU


Tuesday 3 April 2012

Object

An object is a simulation of the real-world entities. These simulated objects are capable of doing all or subset of activities that the real-world entities can do, depending upon the processing power and code that was used to simulate the object.



NOTE: “Designing an object” means designing a class and “Creating an object” means instantiating a class.


For example, consider a programer writing code for Car Race game. He is actually simulating
the real-world entity Car. He is now having a car object. This car object, he simulated, is capable of moving front, suddenly stop while moving, accelerate or decelerate the speed etc. But it is not capable to make you feel the odor of the smoke. The idea is, objects are simulation of real-world entities. Objects make programming easier. Objects are designed by programmer. Hence ultimately, programmer should design the object appropriately to make the programming easier.

To work with objects, we should be able to identify three key characteristics of objects:

  •  The object's behavior— what can you do with this object, or what methods can you apply to    it?
  •  The object's state— how does the object react when you apply those methods?
  •  The object's identity— how is the object distinguished from others that may have the same behavior and state?

Object's behavior: Objects have behavior. Behavior means what an object can do or more specifically how an object will respond for a particular thing. The behavior of an object is simulated via methods. The most obvious behavior of a car object is moving front. A programmer can simulate this in a method named moveFront().

Object's state: The combination of the values for all the attributes of an object is called the object’s state. The state of an object is achieved by the member fields. A programmer can have a field named isMoving to represent the moving state of the car object. See how the field fuelLevel change the reaction of the car object.

class Car{
public float fuelLevel;
                            //represent how much fuel is left in the fuel tank
public String no;
Car(String no){
this.no = no;
}
public void move(float speed){
//code simulating move
if(fuelLevel >1.0)
System.out.println(“Car Moving at speed “ + speed);
else //field fuelLevel changes the behaviour
System.out.println(“Car can't move:Low fuel level);
}
}

Object's identity: Even though many objects are of same type and same behavior, individually they are all different. So, we should have certain way to distinguish them. Java uses instances to achieve this. In the car race example car A, car B and car C are all have same behavior, are all of same type, but all are individually different. The instance of car A uniquely represents car A. new operator is used to instantiate an object.

Car myCar = new Car();
Car myNewCar = new Car();

myCar and myNewCar will never be equal and they uniquely represent two cars. Object Relationships.The most common relationships between classes are

• Aggregation ("has–a")
• Inheritance ("is–a")

We often use the phrases “is a” and “has a” while speaking. For example car is a vehicle, and
car has a music system. 

Aggregation (has-a) Relationship has-a relationship can be achieved by having a reference for a class in another class. Design a class MusicSystem and have a reference to it in the class Car.

Inheritance (is-a) Relationship Is-a relationship can be achieved by inheritance. Just create a class Vehicle, next create another class Car by extending Vehicle.

No comments:

Post a Comment