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 Triply Linked List
HashSet trong Java hoạt động như thế nào?
Java Program to Implement PriorityQueue API
Spring Boot - Tracing Micro Service Logs
Java Program to Describe the Representation of Graph using Incidence Matrix
Java Program to Check Multiplicability of Two Matrices
Spring Boot - Application Properties
Giới thiệu Json Web Token (JWT)
Spring Data JPA @Modifying Annotation
Finding Max/Min of a List or Collection
Guide to Selenium with JUnit / TestNG
Multipart Upload with HttpClient 4
Lập trình đa luồng trong Java (Java Multi-threading)
Spring Boot - Cloud Configuration Client
Java Program to Represent Graph Using Incidence Matrix
Create a Custom Auto-Configuration with Spring Boot
Java Program to Implement Hamiltonian Cycle Algorithm
Guide to Java Instrumentation
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Java Program to Solve any Linear Equations
Configure a Spring Boot Web Application
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Java Program to Check if a Given Binary Tree is an AVL Tree or Not
Examine the internal DNS cache
Entity To DTO Conversion for a Spring REST API
Serialize Only Fields that meet a Custom Criteria with Jackson
Flattening Nested Collections in Java
Receive email using IMAP
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Implement Depth-limited Search
Java Program to Generate a Sequence of N Characters for a Given Specific Case
Java Program to implement Bit Set