1. About this Java Reflection Example
In this example, we will retrieve the access modifiers of a Type through Java Reflection. Then, we will also retrieve base class of a type followed by all the interfaces implemented by it. Now, we will look at the depiction below:

Here, the type we will use is Products class. This class has two modifiers set to it. They are public and final. Public states the class can be instantiated anywhere with no restriction and final states that Products class allows no further inheritance. We derived the Products class from Organization class and we retrieve that information also through java reflection. Finally, we will retrieve all the interface implemented in the Products class. In our case, the implemented interfaces are IOrg and IProduct.
2. IOrg and IProduct Interfaces
First, let us define the interfaces for this example. Note, all the code that we will write here do nothing as it is just for exploring java reflection. The IOrg is defined in the IOrg.java and IProduct is in the IProduct.java. Below is the interface code:
1 2 3 4 5 6 7 8 9 10 11 |
//IOrg.java public interface IOrg { public void IOrgMethod(); } //IProduct.java public interface IProduct { public void IProductMethod(); } |
3. Class Modifiers of Organization and Products Class
We define Organization class in the file Organization.java. This Organization class acts as the base class for the Products and we will define it next. Code for the Organization class is below:
1 2 3 4 5 |
//Organization.java public class Organization { int SomeMember; } |
The Products class has Organization as a base class. It also implements two interfaces which we defined in the earlier section. We will use Java Reflection on the Products class and will retrieve below details:
- Type Modifiers such as public and final
- Base class and in our case, it is Organization
- All the interfaces it implements.
We define the Products class in a file called Products.java and code is given below:
1 2 3 4 5 6 7 8 9 10 11 12 |
//Products.java public final class Products extends Organization implements IOrg, IProduct { private int SomeMember; public void IOrgMethod() { } public void IProductMethod() { } } |
The complete Type relationship is shown below:

4. Get Class Modifiers Using getModifiers()
We use the getModifier function of the Java Reflection Class to get all the modifier of a class object. It returns a number and we need to do bit-wise operation to test against a specific modifier. In our case, we will test object of the product class is public or not. Similarly, we will do a test and take a judgment of if the class is inheritable. Recall, the class marked with Final modifier is not inheritable.
First, we create the instance of a Products class and store that in a reference called, prod. Below is the code for it:
1 2 3 4 |
public static void main(String[] args) { //Sample 01: Create Product class instance Products prod = new Products(); |
We use the prod reference to get the Java Reflection Class for Products by calling the getClass method on it. Once we have the Class, we call getModifiers method to get all the Modifier bits packed in a single integer variable. In our example, we are storing it in the variable called Modifiers and later we will do our bit-wise test on this variable. The code for this is below:
1 2 3 |
//Sample 02: Get Modifiers for the Class Class<?> prodClass = prod.getClass(); int Modifiers = prodClass.getModifiers(); |
The Modifier class of the Java Reflection package declares all the Modifier Constants. For example, if we want to check whether a class instance is public, we need to perform Bit-wise AND on the number returned by the getModifiers call with the constant PUBLIC. If the integer has public bit set in it; we get a value which is greater than zero. This kind of test will tell us a class has a modifier or not.
In the below code snippet, we check the Products class is public and also Final. Note that while performing a Wit-wise AND, we are making use of the constants like Modifier.FINAL to check the class is Final or not. In the same way, we make use of Modifier.PUBLIC to decide a class public or private or protected.
1 2 3 4 5 6 7 |
//Sample 03: Check Product class is Public or Not if ( (Modifiers & Modifier.PUBLIC) > 0) System.out.println("Product Class is Public"); if ( (Modifiers & Modifier.FINAL) > 0) System.out.println("Product Class is Final"); else System.out.println("Product Class can be Inherited"); |
5. Getting Base Class Modifiers Using getSuperclass
We can get a base class of a class by calling the <strong>getSuperclass</strong> method on the Reflection Class instance. In the below code we use prodClass Class instance and get its base class by calling the getSuperclass. Once we have the base Class, we are using the same modifier test which we used in the previous section. The code is below:
1 2 3 4 5 6 7 8 9 10 |
//Sample 04: Get the Organization class Modifiers Class<?> orgClass = prodClass.getSuperclass(); Modifiers = orgClass.getModifiers(); if ( (Modifiers & Modifier.PUBLIC) > 0) System.out.println("Organization Class is Public"); if ( (Modifiers & Modifier.FINAL) > 0) System.out.println("Organization Class is Final"); else System.out.println("Organization Class can be " + "Inherited"); |
6. Get all Implemented Interfaces & Its Type Modifiers
One can call the getInterfaces method on the run-time class using Java Reflection. It returns the array of interfaces implemented by the run-time Class object. For example, the Products class implements two interfaces and when we call this method on it, we get these two interfaces packed in an array.
Before we proceed further, we will have a look at the code given below. In the below code we get the Interface array of the Products class. After that we iterate through it to list all the modifiers. Note, this time we are not checking for a specific Modifier like we did earlier and despite that, we are getting the modifiers as usual by calling getModifiers. And then we give that to Modifier.toString method to list all its modifiers. Below is the code snippet:
1 2 3 4 5 6 7 8 9 10 |
//Sample 05: Let us get all the interface implemented by //the Product class Class<?>[] lstInterfaces = prodClass.getInterfaces(); for (Class<?> Interface:lstInterfaces) { System.out.println("Interface :" + Interface.getSimpleName()); System.out.println("List of Modifiers =>" + Modifier.toString(Interface.getModifiers())); } |
The complete code example from Main.java is below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import java.lang.reflect.Modifier; public class Main { public static void main(String[] args) { //Sample 01: Create Product class instance Products prod = new Products(); //Sample 02: Get Modifiers for the Class Class<?> prodClass = prod.getClass(); int Modifiers = prodClass.getModifiers(); //Sample 03: Check Product class is Public or Not if ( (Modifiers & Modifier.PUBLIC) > 0) System.out.println("Product Class is Public"); if ( (Modifiers & Modifier.FINAL) > 0) System.out.println("Product Class is Final"); else System.out.println("Product Class can be " + "Inherited"); //Sample 04: Get the Organization class Modifiers Class<?> orgClass = prodClass.getSuperclass(); Modifiers = orgClass.getModifiers(); if ( (Modifiers & Modifier.PUBLIC) > 0) System.out.println("Organization Class " + "is Public"); if ( (Modifiers & Modifier.FINAL) > 0) System.out.println("Organization Class " + "is Final"); else System.out.println("Organization Class " + "can be Inherited"); //Sample 05: Let us get all the interface // implemented by //the Product class Class<?>[] lstInterfaces = prodClass.getInterfaces(); for (Class<?> Interface:lstInterfaces) { System.out.println("Interface :" + Interface.getSimpleName()); System.out.println( "List of Modifiers =>" + Modifier.toString( Interface.getModifiers()) ); } } } |
Output

Categories: Java
Tags: Modifier.toString, Reflection getClass, Reflection getInterfaces, Reflection getModifiers, Reflection getSuperClass