Concept of Objects: State
Definition
State of an object is the collection of values of all its attributes or data members at a specific point in time.
In simple words, the state tells us what condition the object is in right now.
For example:
- A
Carobject may have state such as: - color = red
- speed = 60 km/h
-
gear = 3
-
A
Studentobject may have state such as: - name = Asha
- rollNo = 21
- marks = 89
The state is usually stored in the object’s instance variables, and it can change during the lifetime of the object.
Main Content
1. Object and Attributes
- An object is a runtime entity that combines state and behavior.
- The state of an object is represented by its attributes, fields, or data members.
- Each object has its own separate state, even if it belongs to the same class.
Example:
Class: Student
Object 1:
name = Ravi
age = 20
marks = 85
Object 2:
name = Neha
age = 21
marks = 92
Here, both objects are created from the same class Student, but each has a different state.
ASCII representation:
Student Class
|
-------------------------
| |
Object 1 Object 2
name = Ravi name = Neha
age = 20 age = 21
marks = 85 marks = 92
Important points:
- State is not the same as class definition.
- A class is a blueprint, while state belongs to actual objects.
- The same class can produce many objects with different states.
2. State and Memory Representation
- When an object is created, memory is allocated for its attributes.
- The values stored in that memory make up the object’s current state.
- State exists as long as the object exists in memory.
Example:
BankAccount object
-------------------
accountNumber = 1001
balance = 4500
ownerName = "Rahul"
Here, the state is the set of all values currently stored in the object.
Important points:
- Changing attribute values changes the state.
- State is dynamic, meaning it may change over time.
- Object identity remains the same even if its state changes.
Example of state change:
Before deposit:
balance = 4500
After deposit of 1000:
balance = 5500
The object is still the same bank account, but its state has changed.
3. State, Behavior, and Encapsulation
- Object behavior refers to the actions or methods an object can perform.
- Behavior often depends on the current state of the object.
- Encapsulation keeps the state private and allows it to be changed only through methods.
Example:
class BankAccount
{
private int balance;
public void deposit(int amount)
public void withdraw(int amount)
public int getBalance()
}
Here:
balanceis the state.deposit()andwithdraw()are behaviors.privateaccess ensures the state is protected.
How state and behavior work together:
- If
balance > 0, withdrawal may be allowed. - If
balance = 0, withdrawal may be rejected. - If
balancechanges, method outcomes may also change.
Important points:
- State should not usually be accessed directly from outside the object.
- Methods provide controlled access to state.
- Encapsulation maintains data integrity and prevents invalid states.
Example of invalid state prevention:
A Student object should not have marks = -10
Such invalid data should be blocked through validation inside methods.
Working / Process
1. Create the object
- A class blueprint is used to create an object.
- Memory is allocated for the object’s attributes.
2. Assign initial values
- The object’s attributes receive values through constructors, setters, or initialization.
- These values form the initial state.
3. Use methods to change or read state
- Methods modify the state in controlled ways.
- The object may respond differently depending on its current state.
Example process:
Create Account -> Set balance = 5000 -> Deposit 2000 -> Balance becomes 7000
Another example:
Create Student -> Set marks = 78 -> Update marks = 88 -> State changes
This process shows that the state is not fixed forever; it evolves during the object’s lifetime.
Advantages / Applications
- Helps model real-world entities accurately by representing their changing conditions.
- Supports encapsulation by hiding internal data and allowing safe access through methods.
- Makes programs more flexible because different objects of the same class can have different states.
- Useful in applications such as banking systems, student record systems, inventory management, game characters, and medical records.
- Improves maintainability because state can be modified without changing the external interface of the object.
- Enables dynamic behavior, where the output or response of an object depends on its current state.
Examples of applications:
Banking system
- account balance, account status, transaction history
Student system
- name, roll number, marks, attendance
Game development
- health, score, position, level
Library system
- book availability, issue status, borrower details
Summary
- The state of an object is the current set of values stored in its attributes.
- State can change during the lifetime of an object.
- Encapsulation protects state and controls how it is accessed and modified.
- Important terms to remember: object, attribute, field, data member, state, encapsulation.