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:

Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Program to Implement Segment Tree
Java Program to Implement a Binary Search Tree using Linked Lists
Collection trong java
Java Program to Solve Knapsack Problem Using Dynamic Programming
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Java Program to Find SSSP (Single Source Shortest Path) in DAG (Directed Acyclic Graphs)
How to Set TLS Version in Apache HttpClient
Hướng dẫn Java Design Pattern – Iterator
Hướng dẫn sử dụng luồng vào ra ký tự trong Java
Comparing Arrays in Java
Logout in an OAuth Secured Application
Spring @RequestParam Annotation
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Implement Queue using Two Stacks
Spring Security Custom AuthenticationFailureHandler
Logging a Reactive Sequence
Java Program to Implement Sparse Array
Interface trong Java 8 – Default method và Static method
Java Program to Implement the Vigenere Cypher
Java Program to Implement Bellman-Ford Algorithm
Semaphore trong Java
Java Program to Implement HashTable API
Spring Boot - Bootstrapping
Lớp Collections trong Java (Collections Utility Class)
Java Program to Perform the Sorting Using Counting Sort
Introduction to Using FreeMarker in Spring MVC
Java Program to Use Dynamic Programming to Solve Approximate String Matching
Java Program to Check whether Graph is a Bipartite using DFS
Using Spring ResponseEntity to Manipulate the HTTP Response
Creating a Custom Starter with Spring Boot
New Features in Java 12