Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Interface as labels a bad practice in java OO?

During the parsing of certain xml files, I come across a situation that I have to use interface as labels to identify that certain tags belong to certain category, for example, I created a Tag interface to identify that these classes are used to represent xml tags, and ContainableTag to point out that certain tags can be one of the children tags of some tags.

Then I stumble into this page: http://xahlee.org/java-a-day/interface.html (Please look for the "Interface as Labels" session.). It says:

The gist of the problem is that it is a piece of mathematical irrelevance in the language. As a labeling mechanism in a language, for the possible benefit from the software engineering perspective, then it should not be designed as part of the Class Interface, since the concept of labeling, and concept of programing interface, are semantically disparate.

So is interface as labels necessarily a bad practice? As a java programmer, do we have some other alternatives?

like image 272
Winston Chen Avatar asked Sep 17 '09 04:09

Winston Chen


People also ask

Are labels bad in Java?

Labels are fine to break out of nested for-loops. I'd suggest to put the nested loops in a separate method and then break out with return . The problem is that the complex flows of processing becomes really hard to follow.

Should you always code to an interface?

Interfaces aren't a prerequisite for writing code that works, but they are incredibly important when you come to write code as part of a bigger application. The problem with application development is, things will change as the application evolves over time.

Is interface in Java useful?

Why do we use an Interface? It is used to achieve total abstraction. Since java does not support multiple inheritances in the case of class, by using an interface it can achieve multiple inheritances. It is also used to achieve loose coupling.

What is not allowed in an interface Java?

Interfaces can declare only Constant. Instance variables are not allowed. This means all variables inside the Interface must be public, static, final.


2 Answers

Interfaces as markers have largely been replaces by the annotation mechanism in Java 5 or later. They allow you to add arbitrary meta-data. If your interfaces are empty and are only serving to act as class markers, then you should be using annotations instead.

like image 144
Jherico Avatar answered Nov 03 '22 00:11

Jherico


While annotations may provide an alternative for what marker interfaces accomplish, they are only available in Java, and do not integrate well with IDEs: I also use marker interfaces to tag related concepts in my project, and I can then use the type hierarchy browser to find all members (I guess this will be eventually supported by major IDEs for annotations soon).

As for the article you mention, I do not see the point in arguing that if a class syntactically/structurally "fulfills" an interface, that interface may/should be applied automatically to the class ("Any class can declare it [RandomAccess] as a interface..."). That's backwards thinking, in my opinion.

I would argue that the places, where such a marker interface is used in an `instanceof' statement, use inverted logic, but as long as you are stuck within a language without multiple inheritance, aspects and annotations, I see no better way to accomplish this without stepping out of the base language.

Also note that this dead-beat argument about an empty interface always being applicable could also be applied to annotations.

like image 41
Volker Stolz Avatar answered Nov 03 '22 00:11

Volker Stolz