Java Classes and Objects

Building blocks of object-oriented programming

🏗️ What are Classes and Objects?

Classes are blueprints that define properties and behaviors. Objects are instances created from classes. Think of a class as a cookie cutter and objects as the actual cookies made from it.


// Simple class example
class Car {
    String brand = "Toyota";
    
    void start() {
        System.out.println("Car is starting!");
    }
}
                                    

Key Concepts

📋

Class

Blueprint or template for objects

class Student {
    String name;
    int age;
}
🎯

Object

Instance of a class

Student john = new Student();
🏷️

Attributes

Variables inside a class

String name;
int age;
⚙️

Methods

Functions inside a class

void study() {
    System.out.println("Studying...");
}

🔹 Creating Your First Class

Let's create a simple Person class with basic attributes and methods:

// Person.java
public class Person {
    // Attributes (instance variables)
    String name;
    int age;
    String city;
    
    // Method to display person info
    void displayInfo() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("City: " + city);
    }
    
    // Method to celebrate birthday
    void haveBirthday() {
        age++;
        System.out.println("Happy Birthday! Now " + age + " years old.");
    }
}

Key Points:

  • class keyword defines a new class
  • Attributes store data about the object
  • Methods define what the object can do
  • public makes the class accessible from other files

🔹 Creating and Using Objects

Once you have a class, you can create objects (instances) from it:

// Main.java
public class Main {
    public static void main(String[] args) {
        // Creating objects
        Person person1 = new Person();
        Person person2 = new Person();
        
        // Setting attributes
        person1.name = "Alice";
        person1.age = 25;
        person1.city = "New York";
        
        person2.name = "Bob";
        person2.age = 30;
        person2.city = "London";
        
        // Calling methods
        person1.displayInfo();
        person1.haveBirthday();
        
        System.out.println("---");
        
        person2.displayInfo();
        person2.haveBirthday();
    }
}

Output:

Name: Alice
Age: 25
City: New York
Happy Birthday! Now 26 years old.
---
Name: Bob
Age: 30
City: London
Happy Birthday! Now 31 years old.

🔹 Multiple Objects Example

You can create multiple objects from the same class, each with different data:

// Dog.java
class Dog {
    String breed;
    String color;
    int age;
    
    void bark() {
        System.out.println("Woof! Woof!");
    }
    
    void sleep() {
        System.out.println(breed + " is sleeping...");
    }
}

// DogExample.java
public class DogExample {
    public static void main(String[] args) {
        // Create three different dogs
        Dog dog1 = new Dog();
        Dog dog2 = new Dog();
        Dog dog3 = new Dog();
        
        // Set different attributes for each dog
        dog1.breed = "Golden Retriever";
        dog1.color = "Golden";
        dog1.age = 3;
        
        dog2.breed = "Bulldog";
        dog2.color = "White";
        dog2.age = 5;
        
        dog3.breed = "Beagle";
        dog3.color = "Brown";
        dog3.age = 2;
        
        // Each dog can perform the same actions
        dog1.bark();
        dog1.sleep();
        
        dog2.bark();
        dog2.sleep();
        
        dog3.bark();
        dog3.sleep();
    }
}

Output:

Woof! Woof!
Golden Retriever is sleeping...
Woof! Woof!
Bulldog is sleeping...
Woof! Woof!
Beagle is sleeping...

🔹 Class vs Object Summary

Class:

  • Template or blueprint
  • Defines attributes and methods
  • Does not consume memory until object is created
  • Created once

Object:

  • Instance of a class
  • Has actual values for attributes
  • Consumes memory
  • Can create multiple objects from one class

🧠 Test Your Knowledge

What keyword is used to create a new object in Java?