• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java What are the potential difficulties to consider while implementing a marker interface in Java?

Hilton D

For example, let's say I have a class hierarchy of animals, and I want to mark some of them as endangered. I can create a marker interface called "Endangered" that I saw in a similar article by scaler like this:
public interface Endangered {
  // empty interface, no methods declared
Then I can have several classes that implement the Endangered interface:
public class Tiger implements Endangered {
  // class implementation

public class Panda implements Endangered {
  // class implementation
I can now use the Endangered interface to declare instances of the Tiger and Panda classes as endangered:
Tiger tiger = new Tiger();
if (tiger instanceof Endangered) {
  // tiger is endangered

Panda panda = new Panda();
if (panda instanceof Endangered) {
  // panda is endangered
However, there are certain possible concerns to consider while employing marker interfaces:

Because Java does not enable multiple inheritances, a class can only implement one marker interface at a time. This may restrict the design's flexibility and extension.
That’s a misunderstanding. A class can only extend from one parent, but it CAN implement any number of interfaces.

See the Java Language Spec. or try
interface A{}
interface B{}
public class MyClass implements A,B {
    public static void main(String... args) {
        var x = new MyClass();
        System.out.println(x instanceof A);
        System.out.println(x instanceof B);
Last edited:
I don't really see how an interface is the easiest or proper way to implement an 'endangered' class property. This would require a recompile when a species becomes (or ceases to be) endangered. Why not have a static class member that can be set or unset programmatically ? Am I missing something ?
I agree. Bring endangered is subject to change, so needs must be implemented by a variable updatable at run time.
Marker interfaces are useful for class attributes that are fixed at compile time (eg Serialisable) and have the big advantage that the compiler can then check for any errors related to their use.
Top Bottom