Lesson 11 – Classes & Objects

Java is an object-oriented programming language. This means Java programs are built using classes and objects.

A class is a blueprint (like a template), and an object is created from that class.


1. Creating a Simple Class

class Car {
    String brand = "Toyota";
    int year = 2022;
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();

        System.out.println(myCar.brand);
        System.out.println(myCar.year);
    }
}
        

Explanation: - The class Car has two variables - myCar is an object of type Car - We access properties using myCar.brand


2. Creating Multiple Objects

class Student {
    String name;
    int age;
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();

        s1.name = "Khalid";
        s1.age = 25;

        s2.name = "Ayesha";
        s2.age = 22;

        System.out.println(s1.name + " - " + s1.age);
        System.out.println(s2.name + " - " + s2.age);
    }
}
        

3. Methods Inside a Class

class Person {
    void speak() {
        System.out.println("Hello, I am speaking!");
    }
}

public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        p.speak();
    }
}
        

This example shows how to create behaviour inside a class.


4. Methods That Use Variables

class Dog {
    String name;

    void bark() {
        System.out.println(name + " says: Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();

        d.name = "Rocky";
        d.bark();
    }
}
        

5. Returning Values From Methods

class Calculator {
    int square(int x) {
        return x * x;
    }
}

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();

        int result = calc.square(5);
        System.out.println(result);
    }
}
        

6. Multiple Methods in a Class

class Maths {

    int add(int a, int b) {
        return a + b;
    }

    int multiply(int a, int b) {
        return a * b;
    }
}

public class Main {
    public static void main(String[] args) {
        Maths m = new Maths();

        System.out.println("Add: " + m.add(4, 3));
        System.out.println("Multiply: " + m.multiply(4, 3));
    }
}
        

7. Objects Calling Objects

class Engine {
    void start() {
        System.out.println("Engine started...");
    }
}

class Car {
    Engine e = new Engine();

    void drive() {
        e.start();
        System.out.println("Car is moving...");
    }
}

public class Main {
    public static void main(String[] args) {
        Car c = new Car();
        c.drive();
    }
}
        

This shows how objects can be connected to build bigger systems.


8. Using Classes for Real-World Models

class BankAccount {
    String owner;
    double balance = 0;

    void deposit(double amount) {
        balance += amount;
    }

    void showBalance() {
        System.out.println(owner + " has $" + balance);
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount acc = new BankAccount();

        acc.owner = "Khalid";
        acc.deposit(1500);
        acc.deposit(500);
        acc.showBalance();
    }
}
        

Summary

Back to Top