Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Java FAQ: Top Questions

7. What is the difference between a class and an object?

Class: A blueprint or a template that defines the characteristics (attributes/fields) and behaviors (methods/functions) that its objects will have. It's a logical construct and does not occupy memory when created. Think of it like the architectural plans for a house. The plans describe what the house will look like, its rooms, and features, but the plans themselves are not the physical house.

Object: An instance of a class. It is a real-world entity that has a state (values of its attributes) and behavior (actions performed by its methods). Objects are created from classes using the new keyword and occupy memory. Continuing the analogy, an object is the actual house built from the architectural plans. Each house built from the same plans might have different paint colors, furniture, or occupants, but they all follow the same blueprint.

Let's illustrate with a Java code example:


// This is a Class definition
class Dog {
    // Attributes (state) - These define the properties of a Dog
    String name;
    String breed;
    int age;
    String color;

    // Constructor - A special method used to initialize new objects of this class
    public Dog(String name, String breed, int age, String color) {
        this.name = name;      // 'this' refers to the current object's attribute
        this.breed = breed;
        this.age = age;
        this.color = color;
        System.out.println("A new Dog object named " + name + " has been created!");
    }

    // Methods (behavior) - These define what a Dog can do
    public void bark() {
        System.out.println(name + " says Woof! Woof!");
    }

    public void eat(String food) {
        System.out.println(name + " is happily eating " + food + ".");
    }

    public void sleep() {
        System.out.println(name + " is sleeping soundly.");
    }

    // A method to display all information about the dog
    public void displayInfo() {
        System.out.println("--- Dog Information ---");
        System.out.println("Name: " + name);
        System.out.println("Breed: " + breed);
        System.out.println("Age: " + age + " years");
        System.out.println("Color: " + color);
        System.out.println("-----------------------");
    }
}

public class ClassAndObjectExample {
    public static void main(String[] args) {
        // Creating Objects (instances) of the Dog class
        // 'new Dog(...)' calls the constructor and creates a new Dog object in memory.
        Dog myDog = new Dog("Buddy", "Golden Retriever", 3, "Golden"); // myDog is an object
        Dog anotherDog = new Dog("Lucy", "Labrador", 5, "Black");     // anotherDog is another object
        Dog strayDog = new Dog("Max", "German Shepherd", 2, "Brown"); // strayDog is yet another object

        System.out.println("\n--- Interacting with myDog (Buddy) ---");
        myDog.displayInfo(); // Calling a method on the myDog object
        myDog.bark();
        myDog.eat("kibble");

        System.out.println("\n--- Interacting with anotherDog (Lucy) ---");
        anotherDog.displayInfo(); // Calling a method on the anotherDog object
        anotherDog.sleep();
        anotherDog.bark();

        System.out.println("\n--- Interacting with strayDog (Max) ---");
        strayDog.displayInfo();
        strayDog.eat("a bone");
    }
}
                

In the example above:

  • Dog is the class. It serves as the blueprint for creating dog objects. It defines that every dog will have a name, breed, age, and color, and can perform actions like bark(), eat(), and sleep().
  • myDog, anotherDog, and strayDog are objects. They are concrete, distinct instances of the Dog class. Each object has its own unique set of attribute values (e.g., myDog has "Buddy", "Golden Retriever", 3, "Golden", while anotherDog has "Lucy", "Labrador", 5, "Black"). When these objects are created with new Dog(...), memory is allocated for them to store their specific state.

Summary Table: Class vs Object

Feature Class Object
Nature A blueprint, template, or prototype for creating objects. It defines the structure and behavior. A real-world entity, a concrete instance of a class. It represents a specific item based on the class's blueprint.
Memory Allocation Does not consume memory when created (only its definition/metadata is loaded into memory). No memory is allocated for its attributes until an object is created. Consumes memory when created. Memory is allocated on the heap to store its specific attribute values and provide access to its methods.
Existence Exists only logically. It's a conceptual definition. Exists physically in memory during program execution. It's a tangible entity.
Declaration/Creation Declared once using the class keyword (e.g., class Dog { ... }). Created multiple times using the new keyword and a constructor (e.g., Dog myDog = new Dog("Buddy", "Golden Retriever", 3, "Golden");).
Number of Instances There is typically only one definition of a class. You can create multiple, independent objects (instances) from a single class. Each object is unique.
Example Analogy The cookie cutter (defines the shape). The actual cookies baked from the cutter (each cookie is a distinct instance).