Behavior & Identity of an object

Comprehensive study notes, diagrams, and exam preparation for Behavior & Identity of an object.

Behavior & Identity of an Object

Definition

Behavior is the set of actions, methods, or services an object can perform or provide.
Identity is the unique existence of an object that distinguishes it from all other objects, even if some or all of their attribute values are the same.

In simple terms:

Behavior = what the object can do

Identity = who or what the object is

These two concepts are central to object-oriented programming because they help separate:

  • the interface of an object, which exposes behavior,
  • from the implementation and internal data, which define how the object works internally.

Main Content

1. Behavior of an Object

Meaning of behavior

  • : Behavior refers to the methods and operations associated with an object. It represents the tasks the object can perform, the ways it can respond to messages, and the services it offers to other parts of the program. In object-oriented thinking, behavior is often modeled as verbs. For example, a BankAccount object may deposit(), withdraw(), and checkBalance().

Examples and importance

  • : Behavior is what makes objects useful in a program. A Student object may registerCourse(), submitAssignment(), and calculateGPA(). A Printer object may print(), scan(), and cancelJob(). These behaviors define how objects interact with the world and with other objects. Without behavior, an object would just be stored data with no functionality.

2. Identity of an Object

Meaning of identity

  • : Identity is the unique reference or existence of an object. It answers the question, “Which exact object is this?” Even if two objects have identical values in all fields, they can still be different objects because their identities are different. In memory-based languages, identity is often tied to a memory reference or object reference.

Examples and importance

  • : Suppose two Employee objects both have the same name, age, and salary. They may still represent two different employees and therefore have different identities. Identity matters when programs need to track specific instances, compare references, manage object lifecycles, or update a particular object without affecting another similar object. For example, two identical Book objects in a library system may still be different copies, each with its own object identity.

3. Relationship Between Behavior, Identity, and State

State versus behavior versus identity

  • : An object usually has three major characteristics:
  • Identity: the unique object itself
  • State: the data or attribute values stored in the object
  • Behavior: the actions the object can perform Identity remains constant for the object’s lifetime, while state can change, and behavior defines how the object acts or reacts. For example, a TrafficLight object may have the same identity throughout its life, but its state may change from RED to GREEN, and its behavior may include changeColor() or isStopSignal().

Why this distinction matters

  • : If programmers confuse identity with state, they may wrongly assume that two objects with equal data are the same object. If they confuse behavior with state, they may store procedures as data or make classes harder to understand. Good design keeps these concepts clear so that objects remain well-structured and easier to maintain.

Example of Behavior and Identity in Code

class Car {
    String model;
    int speed;

    void accelerate() {
        speed += 10;
    }

    void brake() {
        speed -= 10;
    }
}

In this example:

  • model and speed are part of the object’s state
  • accelerate() and brake() are part of the object’s behavior
  • Each Car instance has its own identity

If we create:

Car c1 = new Car();
Car c2 = new Car();

then c1 and c2 may contain the same values, but they are still two distinct objects with different identities.

ASCII diagram for object structure

+----------------------+
|       Object         |
+----------------------+
| Identity             |
| State                |
| Behavior             |
+----------------------+

ASCII diagram for two similar objects

Object A                    Object B
+-----------+               +-----------+
| model=BMW |               | model=BMW |
| speed=60  |               | speed=60  |
+-----------+               +-----------+
     |                             |
     +------ different identities --+

Working / Process

1. Create the object

  • A class is defined, and an instance of that class is created.
  • The object receives its own identity.
  • Example: Student s1 = new Student();

2. Assign state and expose behavior

  • The object stores attribute values such as name, age, or balance.
  • Methods define what the object can do.
  • Example: s1.name = "Asha"; s1.study();

3. Interact through methods while identity remains unique

  • Other parts of the program use the object through its methods instead of directly modifying internal details.
  • The object’s state may change over time, but its identity stays the same.
  • Example: a BankAccount object can accept deposits and withdrawals, but it remains the same account object throughout.

Advantages / Applications

Improves object-oriented design

  • : Separating behavior and identity helps programmers create classes that are clear, organized, and easy to extend. It supports encapsulation by hiding implementation details and exposing only useful methods.

Helps in object comparison and management

  • : Identity is useful when distinguishing between different object instances, even if they have the same data. This is important in databases, simulations, games, and GUI programs where exact object tracking matters.

Supports real-world modeling

  • : Behavior and identity make it easier to model real-world entities like customers, vehicles, accounts, and devices. Each entity can have its own actions and unique existence, making software more natural and easier to understand.

Summary

  • Behavior means what an object can do.
  • Identity means what makes an object uniquely itself.
  • Objects are understood best through identity, state, and behavior together.
  • Important terms to remember: behavior, identity, object, state, method, instance, encapsulation