wisconsin
Classes

Classes

Curated by Last modified: 3 min read

A class is a blueprint for creating objects. It defines the properties and behaviors of objects that are created from it. In Java, classes are defined using the class keyword.

class Car {
    // Properties
    String make;
    String model;
    int year;

    // Constructor
    public Car(String make, String model, int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    // Method
    public void drive() {
        System.out.println("Driving the car");
    }
}

In order to use a class, you must create an object of that class (unless used from a static context). An object is an instance of a class. You can create an object using the new keyword followed by the class name and parentheses.

Car myCar = new Car("Honda", "Civic", 2022);

Structure of Classes

Constructors

A constructor is a special method that is called when an object is created. It is used to initialize the object’s state. Constructors have the same name as the class and do not have a return type.

public Car(String make, String model, int year) {
    this.make = make;
    this.model = model;
    this.year = year;
}

Methods

Methods are blocks of code that perform a specific task. They define the behavior of objects in a class. Methods are defined within a class and can be called to perform a specific action.

public void drive() {
    System.out.println("Driving the car");
}

Overloading Methods

Method overloading is a feature that allows a class to have multiple methods with the same name but different parameters. The methods must have different parameter lists (number of parameters, type of parameters, or order of parameters).

public void drive() {
    System.out.println("Driving the car");
}

public void drive(int speed) {
    System.out.println("Driving the car at " + speed + " mph");
}

Tip

As long as the function signature is different, the methods can have the same name.

Default Constructor

If a class does not have any constructors defined, Java provides a default constructor that takes no arguments. If a class has any constructors defined, the default constructor is not provided.

Accessors and Mutators

Accessors (getters) and mutators (setters) are methods that allow you to access and modify the properties of an object. Accessors are used to retrieve the values of the properties, while mutators are used to change the values of the properties.

public String getMake() {
    return make;
}

public void setMake(String make) {
    this.make = make;
}

Access Modifiers

Access modifiers control the visibility of classes, methods, and properties. There are four access modifiers in Java:

ModifierDescription
publicAccessible from any other class.
protectedAccessible from the same package and subclasses.
privateAccessible only within the same class.
defaultAccessible only within the same package.

Source

The existing course note was retained and normalized during the merge.

Related: Topic index.

Linked to this page