Topic 2.1 Understanding Objects and Classes in Java

 

What is an Object?

An object is a specific instance of a class. Think of an object as a real-world thing that has specific properties (attributes) and can perform actions (methods).

Example:

Imagine a car. A car is an object. It has properties like color, model, speed, and fuel level. It can perform actions like starting, stopping, accelerating, and braking.

 

What is a Class?

A class is like a blueprint for creating objects. It defines the properties and behaviors that the objects created from the class will have.

Example:

Continuing with the car analogy, if a car is an object, the blueprint for making a car is the class. The class defines what attributes (like color and model) and methods (like start and stop) every car object will have.

Real-World Example:

Let's take a class called Car. This class will define what every car object can have and do. When we create a new car object from the Car class, we can set its attributes and use its methods.

Blueprint (Class) - Car:

 

Creating an Object:

Using the Car class, we can create a car object with specific attributes.

 

public class Car {

    // Attributes (properties)

    String color;

    String model;

    int speed;

 

    // Method to start the car

    void start() {

        System.out.println("Car is starting.");

    }

 

    // Method to stop the car

    void stop() {

        System.out.println("Car is stopping.");

    }

 

    // Method to accelerate the car

    void accelerate() {

        speed += 10;

        System.out.println("Car is accelerating. Speed: " + speed + " km/h");

    }

}

 

Full Program in Java

Here is a complete program demonstrating the concept of objects and classes. This program creates a car object from the Car class and uses its methods.

public class Main {

    public static void main(String[] args) {

        // Creating a new car object from the Car class

        Car myCar = new Car();

 

        // Setting attributes of the car object

        myCar.color = "Red";

        myCar.model = "Toyota";

        myCar.speed = 0;

 

        // Using methods of the car object

        myCar.start();

        myCar.accelerate();

        myCar.accelerate();

        myCar.stop();

 

        // Displaying the car's attributes

        System.out.println("Car Details:");

        System.out.println("Color: " + myCar.color);

        System.out.println("Model: " + myCar.model);

        System.out.println("Speed: " + myCar.speed + " km/h");

    }

}

 

class Car {

    // Attributes (properties)

    String color;

    String model;

    int speed;

 

    // Method to start the car

    void start() {

        System.out.println("Car is starting.");

    }

 

    // Method to stop the car

    void stop() {

        System.out.println("Car is stopping.");

    }

 

    // Method to accelerate the car

    void accelerate() {

        speed += 10;

        System.out.println("Car is accelerating. Speed: " + speed + " km/h");

    }

}

Explanation of the Program:

  1. Class Definition: The Car class is defined with three attributes (color, model, speed) and three methods (start, stop, accelerate).
  2. Object Creation: In the main method, a Car object named myCar is created.
  3. Setting Attributes: The attributes of myCar are set to specific values (color is set to "Red", model to "Toyota", and speed to 0).
  4. Using Methods: The methods of the myCar object are called to start, accelerate, and stop the car.
  5. Displaying Attributes: The attributes of the myCar object are displayed.

 

Programming Problem:

Create a class called Book that has the following attributes: title, author, and pages. Add methods to read the book (print a message indicating the book is being read) and to close the book (print a message indicating the book is closed).

Your Task:

  1. Define the Book class with the attributes and methods mentioned.
  2. Create an object of the Book class.
  3. Set the attributes of the book object to specific values.
  4. Call the methods to read and close the book.
  5. Print the attributes of the book object.

This exercise will help you understand how to create and use objects in Java. Happy coding!