Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Java Top

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


1. Overview

In this quick tutorial, we'll discuss how we can check if a class is abstract or not in Java by using the Reflection API.

2. Example Class and Interface

To demonstrate this, we'll create an AbstractExample class and an InterfaceExample interface:

public abstract class AbstractExample {

    public abstract LocalDate getLocalDate();

    public abstract LocalTime getLocalTime();

public interface InterfaceExample {

3. The Modifier#isAbstract Method

We can check if a class is abstract or not by using the Modifier#isAbstract method from the Reflection API:

void givenAbstractClass_whenCheckModifierIsAbstract_thenTrue() throws Exception {
    Class<AbstractExample> clazz = AbstractExample.class;

In the example above, we first obtain the instance of the class we want to test. Once we have the class reference, we can call the Modifier#isAbstract method. As we'd expect, it returns true if the class is abstract, and otherwise, it returns false.

It's worthwhile to mention that an interface class is abstract as well. We can verify it by a test method:

void givenInterface_whenCheckModifierIsAbstract_thenTrue() {
    Class<InterfaceExample> clazz = InterfaceExample.class;

If we execute the test method above, it'll pass.

The Reflection API provides an isInterface() method as well. If we want to check if a given class is abstract but not an interface, we can combine the two methods:

void givenAbstractClass_whenCheckIsAbstractClass_thenTrue() {
    Class<AbstractExample> clazz = AbstractExample.class;
    int mod = clazz.getModifiers();
    Assertions.assertTrue(Modifier.isAbstract(mod) && !Modifier.isInterface(mod));

Let's also validate that a concrete class returns the appropriate results:

void givenConcreteClass_whenCheckIsAbstractClass_thenFalse() {
    Class<Date> clazz = Date.class;
    int mod = clazz.getModifiers();
    Assertions.assertFalse(Modifier.isAbstract(mod) && !Modifier.isInterface(mod));

4. Conclusion

In this tutorial, we've seen how we can check if a class is abstract or not.

Further, we've addressed how to check if a class is an abstract class but not an interface through an example.

As always, the complete code for this example is available over on GitHub.

Java bottom

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

Generic footer banner
Inline Feedbacks
View all comments
Comments are closed on this article!