Problem Statement: Creating a Car Class in Java to Understand Object-Oriented Programming (OOP)

Sabat AliSabat Ali
4 min read

New Problem Statement

Question:

  1. Create a class named Car with two attributes: brand and year.

  2. Write a constructor that initializes these attributes using the this keyword.

  3. Create a method called showDetails() to print the car's brand and year of manufacturing.

  4. Create two objects of the Car class with different brands and years.

  5. Call the showDetails() method on both objects.


Breaking Down the Problem

We will walk through each requirement step by step:

  1. Creating a Class: The Car class will have two attributes: brand (for the car's brand name) and year (for the year the car was manufactured).

  2. Using a Constructor: A constructor is a special method in Java used to initialize objects. It will use the this keyword to differentiate between class attributes and constructor parameters.

  3. Method to Show Details: The showDetails() method will be responsible for printing the car’s brand and year.

  4. Creating Objects: We will create two objects of the Car class, each representing a different car with its unique brand and year.

  5. Calling Methods: Finally, we will call the showDetails() method to display information about both cars.


Java Code Example

public class Car {
    // Step 1: Declare the attributes
    String brand;
    int year;

    // Step 2: Constructor to initialize the attributes
    public Car(String brand, int year) {
        this.brand = brand; // "this" refers to the class attribute
        this.year = year;
    }

    // Step 3: Method to display the car details
    public void showDetails() {
        System.out.println("Car Brand: " + brand + ", Year: " + year);
    }

    // Step 4 and 5: Main method to create objects and display their details
    public static void main(String[] args) {
        // Creating two car objects
        Car car1 = new Car("Toyota", 2020);
        Car car2 = new Car("Honda", 2022);

        // Calling showDetails() method for each car
        car1.showDetails();
        car2.showDetails();
    }
}

Explanation of the Code

  1. Class Declaration:

    • We declare a class named Car that will represent a car's details.

    • Inside the class, we define two attributes: brand (a string for the car's name) and year (an integer for the year it was manufactured).

  2. Constructor:

    • A constructor is a special method that is called when an object of the class is created.

    • In the Car class, we have a constructor that takes two parameters, brand and year, and assigns them to the class attributes using the this keyword.

  3. Method:

    • The showDetails() method is used to print the car's information in a readable format. It accesses the brand and year attributes and prints them.
  4. Creating Objects:

    • In the main method, we create two objects, car1 and car2, each initialized with a different brand and year using the constructor.

    • We pass "Toyota", 2020 for the first car and "Honda", 2022 for the second car.

  5. Calling the Method:

    • Finally, we call the showDetails() method on both car1 and car2 objects to print their details.

Output

Car Brand: Toyota, Year: 2020
Car Brand: Honda, Year: 2022

Key Concepts Covered

  1. Classes and Objects:

    • A class in Java is like a blueprint, and objects are instances created from that blueprint. The Car class is a blueprint, and car1 and car2 are objects.
  2. Attributes:

    • These are the properties or characteristics of a class. In our example, brand and year are the attributes of the Car class.
  3. Constructor:

    • A constructor initializes an object with values when it is created. We used a constructor to set the brand and year of the car when creating the objects car1 and car2.
  4. Methods:

    • A method in Java is a block of code that performs an action. The showDetails() method in this case prints the car’s brand and year.
  5. The this Keyword:

    • The this keyword is used inside the constructor to refer to the current object's attributes, helping to distinguish between the attributes and the parameters with the same name.

Conclusion

In this blog, we learned how to create a simple Car class in Java, define attributes, use a constructor to initialize objects, and write a method to display the object's data. This example demonstrates key Object-Oriented Programming concepts like classes, objects, constructors, and methods.

Once you understand these basic concepts, you can easily apply them to create more complex classes and objects to model real-world scenarios. Whether it's for managing car details or representing other entities, Object-Oriented Programming is a powerful tool to organize your code efficiently!


Further Learning

  • Practice Exercises: Try creating another class like Mobile with attributes brand, model, and a method displayDetails(). Create objects for different mobile brands and display their details.

  • Advanced Topics: After mastering basic classes and objects, move on to more advanced OOP concepts like inheritance, polymorphism, and abstraction!

0
Subscribe to my newsletter

Read articles from Sabat Ali directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sabat Ali
Sabat Ali