Token interfaces are slightly anti-patterns in modern Java, required in earlier times due to the lack of the ability to directly add metadata to the class. The βmodernβ approach is annotations:
@Retention(RetentionPolicy.RUNTIME) @interface Flyer { } @Retention(RetentionPolicy.RUNTIME) @interface Swimmer { } @Flyer @Swimmer public class Penguin implements Animal { }
And checking the runtime:
if(Animal.class.isAnnotationPresent(Flyer.class)) { // fly! }
You can use this approach if all you want to know is that Animal has this trait, that is, the ability to fly and swim is pure metadata.
What are you trying to achieve? I would not call this OOP, because the OOP approach, as a rule, does not request opportunities and does not execute conditional logic, but uses polymorphism.
source share