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
To demonstrate this, we'll create an AbstractExample class:
public abstract class AbstractExample {
public abstract LocalDate getLocalDate();
public abstract LocalTime getLocalTime();
}
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:
@Test
void givenAbstractClass_whenCheckModifierIsAbstract_thenTrue() throws Exception {
Class<AbstractExample> clazz = AbstractExample.class;
Assertions.assertTrue(Modifier.isAbstract(clazz.getModifiers()));
}
In the above example, we first obtain the instance of the class we want to test. Once we have the class reference, all we need to do is call the Modifier#isAbstract method. As we'd expect, it returns true if the class is abstract, and otherwise, it returns false.
4. Conclusion
In this tutorial, we've seen how we can check if a class is abstract or not.
As always, the complete code for this example is available over on GitHub.
The post Checking if a Java Class is 'abstract' Using Reflection first appeared on Baeldung.
Image may be NSFW.Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.

Clik here to view.
