Concrete Class in Java

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 HashMapHashSetArrayList, 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 MapList, 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.

Related posts:

A Guide to Apache Commons Collections CollectionUtils
Constructor Injection in Spring with Lombok
A Guide to WatchService in Java NIO2
Java Program to Implement Levenshtein Distance Computing Algorithm
Converting Strings to Enums in Java
Adding Shutdown Hooks for JVM Applications
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Java Program to Implement Park-Miller Random Number Generation Algorithm
Hướng dẫn Java Design Pattern – Flyweight
An Example of Load Balancing with Zuul and Eureka
Thao tác với tập tin và thư mục trong Java
Jackson Exceptions – Problems and Solutions
Java Program to Implement the Checksum Method for Small String Messages and Detect
Spring Boot - Zuul Proxy Server and Routing
Giới thiệu Google Guice – Binding
Java Program to Implement Threaded Binary Tree
HandlerAdapters in Spring MVC
Easy Ways to Write a Java InputStream to an OutputStream
Java Program to Implement Singly Linked List
REST Web service: Upload và Download file với Jersey 2.x
Java Program to Implement Stack using Linked List
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Debugging Reactive Streams in Java
Java Program to Implement SynchronosQueue API
Java Program to Implement Binomial Tree
Kiểu dữ liệu Ngày Giờ (Date Time) trong java
Guide to java.util.concurrent.Future
Simple Single Sign-On with Spring Security OAuth2
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Static Content in Spring WebFlux
Guide to Java OutputStream