Instance

Key points of this
  • It’s the embodiment (entity) of a class (blueprint).
  • It comes up in the discussion of object-oriented
I’ll write it simply.

What is an instance ?

One of the concepts in object-oriented

and

An “entity” that embodies a blueprint (class)

Understanding Instances

Instance is a concept used in object-oriented programming that refers to the “real-world manifestation” of a class, which serves as a blueprint or design.

1. What is Object-Oriented Programming?

Object-oriented programming focuses on “things” and how they behave. It emphasizes defining objects based on their attributes and behaviors. For instance, when creating a game, you might consider the following objects:

  1. Player
  2. Enemy character
  3. Bullet

Each object has specific attributes (what it is like) and behaviors (how it acts). For example:

  • The player loses if hit by bullets three times (attribute).
  • The enemy character moves left on the screen (behavior).
  • The bullet’s color corresponds to its power (attribute).

By defining these objects and their interactions, you build the functionality of the game using an object-oriented approach.

2. Understanding Instances

An instance is the “actual object” created from a class, which serves as a blueprint. In programming terms, creating an instance means using the class to generate a concrete object.

Consider an example where you design a clock:

  • Class (Blueprint): The design of the clock.
  • Instance (Actual Object): The actual clock created from the design.
  • Object (Thing): A broad term that includes both the design and the actual clock.

In essence, the class is the blueprint, the instance is the real object created from that blueprint, and the object is a general term that includes both.

3. Programming Example

Here’s a Java example demonstrating classes and instances:

// Clock class
class Clock {
    // Attribute (Property)
    private int hour;

    // Behavior (Method)
    void setHour(int hour) {
        this.hour = hour;
    }

    int getHour() {
        return this.hour;
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        // Create an instance of Clock named 'myClock'
        Clock myClock = new Clock();

        // Set the hour
        myClock.setHour(10);

        // Display the hour
        System.out.println("The time on the clock: " + myClock.getHour());
    }
}
  • Class: Clock (Blueprint)
  • Instance: myClock (The actual object created)
  • Object: A broad term that includes both the class and the instance

In this example, the Clock class serves as the blueprint, and myClock is the instance created from this blueprint. myClock represents a real object with specific attributes and behaviors.

4. Summary

In simple terms, an “instance” refers to the actual object created from a class. When you encounter the term “instance” in programming, think of it as the concrete object derived from a class blueprint.