Reputation: 1167
Specification states that interface is intended to define the contract for what a class can do and contains a set of methods required to implement. But at the same time, interface may have constants.
For what purpose it is allowed in Java?
What is the point of existence of constants in interface and how can they be used in it? As I understand they can only be taken as arguments by methods. But I do not see much point because interface doesn't say anything about how a class will implement its methods.
Upvotes: 17
Views: 818
Reputation: 38424
One of the usages of such constants to me post-Java 5 (which brought enum
s that are superior to bitmasking and can be declared on interfaces as well) is when the constant is a part of the method's contract.
Consider an interface like this:
public interface Element {
Element NONE = new EmptyElement();
/**
* Looks up an element using this element as a root.
* If the lookup fails, returns Element.NONE.
*/
Element findElements(String searchBy);
}
The constant is a part of the method's contract and should, I think, be therefore in the interface. This is useful in API programming since you can offer default or special return values for the API methods for everyone to use and check against.
The most common case of this prevents the problems with ambiguous null
values returned (e.g. the HashMap#get()
method will return null
both for a missing key and for a null
value), but there is much more to it than just nulls - any special values that can be returned should be, I think, on the interface.
Upvotes: 1
Reputation: 5208
Imagine an application that uses resources such as images or sounds. You may define a common interface for your resources.
interface MyResource {
void load();
void dispose();
// ...
}
Your folder structure may look like the following:
+ Root
|--+ Resources
|--+ Images
|--+ Sounds
|--+ Data
You know that all your resources will be under Root/Resources/
.
This information can be shared and known, not only by your resources, but also by your other application components.
Having this in mind, your interface now becomes:
interface MyResource {
public static final String RESOURCE_ROOT_PATH = "Root/Resources/";
void load();
void dispose();
// ...
}
Your specific implementations, such as an image, may define their own root path, based on the common path for all resources.
class MyImage implements MyResource {
public static final String IMAGE_ROOT_PATH =
MyResource.RESOURCE_ROOT_PATH + "Images/";
...
}
Alternatively, you may look at interfaces like javax.swing.SwingConstants
, which are used to share constants for a certain functionality among implementations.
But, then again, for this sort of thing, I'd rather use an enum
, nowadays.
Upvotes: 2
Reputation: 86411
The constants can be used outside the interface.
For example, they can be used by the classes that implement the interface. This may be useful if a single definition is required across implementation classes.
For example, consider an interface that defines some bitwise-OR style constants that may be used consistently across all the subclasses. Only a single definition of these is required, and only a single set need be learned.
Upvotes: 2
Reputation: 691715
Lets' explain with an example:
public interface People {
/**
* Gets the population for the given type
*/
public Population getPopulationForType(int populationType);
}
Do you find the above contract easy to understand? Or wouldn't the following be better?
public interface People {
int BLACK_POPULATION_TYPE = 0;
int ASIAN_POPULATION_TYPE = 1;
int WHITE_POPULATION_TYPE = 2;
/**
* Gets the population for the given type
*/
public Population getPopulationForType(int populationType);
}
You could have a FrenchPeople, AmericanPeople and ChinesePeople implementation, but all are supposed to return the popuation for all three possible types of people.
Upvotes: 1
Reputation: 974
A constant is part of an interface, too. Constant values are used in design to avoid Magic Numbers, i.e. numbers which have a certain meaning to the implementation, but seem to pop out of nowhere.
There are many cases where numerical values influence the behavior of the code. Consider, for example, an interface for a GUI button. How this button is actually drawn is up to the implementation; but what kind of button it is is part of the contract which forms the interface: Is it a normal push button, has it an image, or is it a checkbox? This behavior can be modified using constants, commonly used by OR'ing values: For example, int buttonType = PUSHBUTTON|IMAGEBUTTON
.
Upvotes: 11
Reputation: 2364
They can be used to give symbolic meaning to the return args/input args of the methods. It has been so pre-enum days.
Upvotes: 0
Reputation: 23208
One possible use of interface constants is to make sure that the constants are visible across all implementation classes. Sure, you can put all those in a class and use the same class in those implementation classes but it's much easier to define the constants at the specification level.
Think of it this way; even though the constants don't define the "implementation", they are there in case any implementation needs them.
Upvotes: 0