Objects and Classes

πŸ” What is a Class?

In Java, a class is a user-defined data type that acts as a blueprint for creating objects. It defines:

  • State (via fields/variables)
  • Behavior (via methods/functions)

It encapsulates data for the object and methods to operate on that data.


🧱 What is an Object?

An object is a runtime instance of a class. It represents an actual entity in the real world with:

  • Actual values (unlike the abstract nature of a class)
  • Ability to invoke methods defined in the class

βœ… Example: Defining and Using a Class in Java

We’ll model a real-world entity: Car


πŸ”§ Car.java – Blueprint (Class)

public class Car {
    // Fields (attributes)
    String color;
    int speed;

    // Method (behavior)
    void drive() {
        System.out.println("The " + color + " car is driving at " + speed + " km/h.");
    }
}

πŸš€ Main.java – Creating and Using Objects

public class Main {
    public static void main(String[] args) {
        // Creating first object
        Car car1 = new Car();
        car1.color = "Red";
        car1.speed = 60;
        car1.drive();  // Output: The Red car is driving at 60 km/h.

        // Creating second object
        Car car2 = new Car();
        car2.color = "Blue";
        car2.speed = 90;
        car2.drive();  // Output: The Blue car is driving at 90 km/h.
    }
}

🧠 Key Concepts Explained

ConceptDescription
Car car1 = new Car();Instantiates a new object using the Car class
car1.color = "Red";Sets the state of the object
car1.drive();Invokes behavior defined in the class

This approach adheres to Object-Oriented Programming (OOP) principles such as encapsulation and modularity.


🎯 Why This Matters

  • Java is inherently object-oriented. Understanding classes and objects is foundational for mastering frameworks like Spring, Hibernate, and even for working with design patterns and architecture.
  • In real-world projects, everything revolves around modeling real or abstract entities using classes.

πŸ“ Homework for Students

Task:

  1. Create a Student class with:

    • name (String)
    • age (int)
    • A method introduce() that prints:
      "Hi, my name is Alex and I am 21 years old."
  2. In your main method:

    • Create two Student objects with different data
    • Call the introduce() method on both

βœ… Let me know if you’d like to proceed to:

  • Constructors in Java, or
  • Methods and Parameters (deeper dive)?