Skip to content
Advertisement

How to create a getter method for an abstract method that is inside an enum in Java?

I have an enum that contains an non-public abstract method and I can not change the visibility of this abstract method. The abstract method is implemented for every single unit inside the enum.

I am trying to access this method from another package for the purposes of unit testing, but I can not because it is not visible in the other package. Is there a way where I can create a getter method for an abstract method?

Is there a different way to do the unit test in a way that does not involve using a getter method or changing the visibility of the abstract method?

Here is an example of the code I am working with:

JavaScript

Advertisement

Answer

There are two ways you can do this. One way, like you suggested in your question, is to use a public method to expose your package-private method. Here’s a short example of such a program:

JavaScript
JavaScript

Output will be:

JavaScript

The other way is to use something called reflection. You mentioned that you wanted a way to access the method without creating a getter method. This is how you can do just that:

JavaScript
JavaScript

Output will be:

JavaScript

Please note that reflection can get out of hand quite quickly, especially if you start renaming methods and/or changing the method headers. Useful for testing, yes, but I would keep it out of production code. Also, a small terminology thing, you refer to “methods” as “functions” in your question. A method is a function that belongs to an Object, so since Java is entirely object-oriented, there are no functions in Java.

Advertisement