Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

In this quick guide, we’ll discuss the term “concrete class” in Java.

First, we’ll define the term. Then, we’ll see how it’s different from interfaces and abstract classes.

2. What Is a Concrete Class?

A concrete class is a class that we can create an instance of, using the new keyword.

In other words, it’s a full implementation of its blueprint. A concrete class is complete.

Imagine, for example, a Car class:

public class Car {
    public String honk() {
        return "beep!";
    }

    public String drive() {
        return "vroom";
    }
}

Because all of its methods are implemented, we call it a concrete class, and we can instantiate it:

Car car = new Car();

Some examples of concrete classes from the JDK are HashMap, HashSet, ArrayList, and LinkedList.

3. Java Abstraction vs. Concrete Classes

Not all Java types implement all their methods, though. This flexibility, also called abstraction, allows us to think in more general terms about the domain we’re trying to model.

In Java, we can achieve abstraction using interfaces and abstract classes.

Let’s get a better look at concrete classes by comparing them to these others.

3.1. Interfaces

An interface is a blueprint for a class. Or, in other words, its a collection of unimplemented method signatures:

interface Driveable {
    void honk();
    void drive();
}

Note that it uses the interface keyword instead of class.

Because Driveable has unimplemented methods, we can’t instantiate it with the new keyword.

But, concrete classes like Car can implement these methods.

The JDK provides a number of interfaces like Map, List, and Set.

3.2. Abstract Classes

An abstract class is a class that has unimplemented methods, though it can actually have both:

public abstract class Vehicle {
    public abstract String honk();

    public String drive() {
        return "zoom";
    }
}

Note that we mark abstract classes with the keyword abstract.

Again, since Vehicle has an unimplemented method, honk, we won’t be able to use the new keyword.

Some examples of abstract classes from the JDK are AbstractMap and AbstractList.

3.3. Concrete Classes

In contrast, concrete classes don’t have any unimplemented methods. Whether the implementations are inherited or not, so long as each method has an implementation, the class is concrete.

Concrete classes can be as simple as our Car example earlier. They can also implement interfaces and extend abstract classes:

public class FancyCar extends Vehicle implements Driveable {
    public String honk() { 
        return "beep";
    }
}

The FancyCar class provides an implementation for honk and it inherits the implementation of drive from Vehicle.

As such, it has no unimplemented methods. Therefore, we can create a FancyCar class instance with the new keyword.

FancyCar car = new FancyCar();

Or, simply put, all classes which are not abstract, we can call concrete classes.

4. Summary

In this short tutorial, we learned about concrete classes and their specifications.

Additionally, we showed the differences between interfaces and concrete and abstract classes.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!