Instances

Comprehensive study notes, diagrams, and exam preparation for Instances.

Instances

Definition

An instance is an individual object created from a class, representing one concrete example of that class with its own memory allocation, attribute values, and behavior as defined by the class.

Example: If Student is a class, then student1 and student2 are instances of that class.


Main Content

1. Instances in Object-Oriented Programming

  • A class is a template, but an instance is the actual object created from that template.
  • Every instance has its own identity, meaning two instances of the same class are separate objects even if they contain similar data.

Example:

  • Class: Car
  • Instances: car1 = Toyota, car2 = Honda

Even though both are cars, each instance can have different values such as color, model, speed, and registration number.

Important characteristics of instances:

Identity

  • : Each instance is unique in memory.

State

  • : The data stored inside the instance.

Behavior

  • : The methods it can perform.

ASCII representation:

Class: Student
       |
       v
+------------------+
| Instance: Alice  |
| name = "Alice"   |
| age = 20         |
+------------------+

+------------------+
| Instance: Bob    |
| name = "Bob"    |
| age = 21         |
+------------------+

Here, both Alice and Bob are instances of the same class, but they have different attribute values.

2. Instances and Encapsulation

  • Instances support encapsulation by keeping data and methods together inside one object.
  • Internal data of an instance is usually protected from direct outside access, and it is changed through methods.

For example, an Account instance may contain:

  • accountNumber
  • balance

Instead of allowing direct modification of balance, the class may provide:

  • deposit(amount)
  • withdraw(amount)

This protects the instance from invalid changes, such as setting a negative balance directly.

Why this matters:

  • It ensures data integrity.
  • It prevents accidental misuse.
  • It makes the program easier to maintain and debug.

Example:

class Account {
    private double balance;

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }
}

Each Account instance stores its own balance, and only the deposit method can safely modify it.

3. Instances and Data Abstraction

  • Data abstraction hides the internal details of how an object works and exposes only what is necessary.
  • An instance represents the usable form of an abstract concept.

For example, a BankAccount class may hide:

  • how the balance is stored
  • how transactions are processed
  • how rules are enforced

But it exposes:

  • checking balance
  • depositing money
  • withdrawing money

This means the user of the instance does not need to know the internal implementation.

Benefits of this relationship:

  • Users focus on what an object does, not how it does it.
  • The class can be changed internally without affecting external code, as long as the public interface stays the same.
  • It improves modularity and reusability.

Example: A Printer instance may have methods like:

  • printDocument()
  • scanDocument()

The user interacts with these methods, while the internal hardware details remain hidden.


Working / Process

1. Define the class

  • A class is written to specify the attributes and methods that instances will have.
  • Example: Student class with name, rollNo, and displayDetails().

2. Create an instance

  • An object is instantiated from the class using a constructor or object creation statement.
  • Example: Student s1 = new Student();
  • At this stage, memory is allocated for the new instance.

3. Use the instance

  • The instance’s fields can be assigned values and its methods can be called.
  • Example:
    • s1.name = "Asha";
    • s1.displayDetails();
  • The instance now represents one concrete object with its own state.

Simple process diagram:

Class blueprint
      |
      v
Object creation
      |
      v
Instance in memory
      |
      v
Set values + call methods

How it works in memory:

  • Each instance gets its own separate space.
  • Two instances of the same class do not overwrite each other’s data.
  • If one instance changes, the other remains unchanged.

Example:

class Book:
    def __init__(self, title):
        self.title = title

b1 = Book("Maths")
b2 = Book("Science")
  • b1 and b2 are separate instances.
  • b1.title is "Maths"
  • b2.title is "Science"

Advantages / Applications

  • Instances make it possible to model real-world entities in software in a natural and organized way.
  • They support encapsulation by combining data and methods, improving security and reducing errors.
  • They support data abstraction by hiding implementation details and exposing only necessary operations.

Applications include:

Banking systems

  • : Each customer account can be an instance with its own balance and transaction history.

Student management systems

  • : Each student is an instance with personal information, marks, and attendance.

Library systems

  • : Each book, member, or loan record can be represented as an instance.

E-commerce systems

  • : Each product, order, and user can be modeled as an instance.

Gaming

  • : Each player, enemy, weapon, or level object can be an instance with independent behavior.

Additional benefits:

  • Easier code organization
  • Better reusability
  • Easier testing of individual objects
  • Clear mapping between program structure and problem domain

Summary

  • Instances are individual objects created from a class.
  • Each instance has its own identity and state.
  • Instances are essential for encapsulation and data abstraction.

Important terms to remember

  • Class
  • Object
  • Instance
  • Encapsulation
  • Data Abstraction