Skip to content

Classes and Objects in Java

In Java, classes and objects are the fundamental components of object-oriented programming. A class is a blueprint or template from which objects are created. In essence, it is a user-defined data type that encapsulates data and methods that can operate on that data.

A class can be seen as an abstraction that defines what data and operations will be associated with objects of that class. Data are represented by the class's attributes or fields, while operations are represented by the class's methods.

For instance, consider a Car class. The class can specify that each Car object will have attributes like color, brand, and speed, and methods such as accelerate, brake, and turn. However, the class itself doesn't contain any specific car's color or speed, just as a blueprint doesn't include the color of a specific house's walls or the type of its doors. Instead, these details are determined when you create an object from the class, which is like building a house from a blueprint.

The following is a simple example of a class declaration in Java:

public class Car {
    // Fields (attributes)
    private String color;
    private String brand;
    private double speed;

    // Methods
    public void accelerate() {
        speed += 10;
    }

    public void brake() {
        if (speed > 0) speed -= 10;
    }

    // ... other methods as needed
}

This Car class serves as a blueprint for creating Car objects. Each Car object created from this class will have its own color, brand, and speed attributes and will be able to accelerate and brake through the defined methods. But remember, these details are only determined when you create an instance (or object) of the class.

A class, therefore, lays out the characteristics and behaviors that are common to all objects of a certain kind, but it's the instantiated objects that hold the actual values.

Constructor

Within a class, you can define a constructor, which is a special type of method used to initialize the state of an object, or in other words, set initial values for the object's attributes. The constructor is called automatically when a new instance of a class is created. It has the same name as its class and doesn't return any value (not even void).

Here is a simple example of a class:

public class Car {
    // Fields (attributes)
    private String color;
    private String brand;
    private double speed;

    // Constructor
    public Car(String color, String brand, double speed) {
        this.color = color;
        this.brand = brand;
        this.speed = speed;
    }

    // Methods
    public void accelerate() {
        speed += 10;
    }

    public void brake() {
        if (speed > 0) speed -= 10;
    }

    // ... other methods as needed
}

In this updated Car class, the constructor Car is defined with three parameters: color, brand, and speed. When you create a new Car object, you can pass these three arguments to the constructor to initialize the car's color, brand, and speed:

Instantiation and Object Independence

In Java, you create an object (or an instance of a class) using the new keyword:

Car myCar = new Car("red", "Toyota", 0);

Here, myCar is an instance of the Car class, with a color of "red", a brand of "Toyota", and an initial speed of 0.

You can create multiple instances of a class, and each instance is completely independent of the others. Let's illustrate this by creating another Car instance:

Car yourCar = new Car("blue", "Ford", 0);

We now have two Car instances: myCar and yourCar. They are both instances of the same Car class, but have their own separate attributes. Changes to one instance do not affect the other. For example:

myCar.accelerate();  // Increases the speed of myCar, not yourCar

This independent nature of objects is a cornerstone of Object-Oriented Programming (OOP), providing a high degree of modularity and reusability in your code.

Tasks

In a new Java file named Week5Task2.java, complete the following tasks:

  1. Define a class Student with the following attributes: name, grade, and major.
  2. Include a constructor that initializes all the attributes.
  3. Create a method study() that prints out "{name} is studying.".
  4. In your main() method, create an object of the Student class and call the study() method.

For extra practice, try creating additional objects and calling various methods.

Answers

Additional Resources

W3 Schools - Java Classes/Objects

Oracle Docs - Defining a Class