Java Magazine, Sept/Oct 2016
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2016 52 new to java in which case they receive the default implementation from the interface Old code continues to work while new code can use this new functionality Static Methods Interfaces can now also contain static methods with implementations These are defined by using the usual static modifier at the beginning of the method signature As always when writing interfaces the public modifier may be left out because all methods and all constants in interfaces are always public So What About the Diamond Problem As you can see abstract classes and interfaces have become quite similar now Both can contain abstract methods and methods with implementations although the syntax is different There are still some diferences for instance abstract classes can have instance fields whereas interfaces cannot but these still leave the central point since the release of Java 8 you have multiple inheritance via interfaces that can contain code At the beginning of this article I pointed out how the Java designers treaded very carefully to avoid multiple code inheritance because of possible problems mostly related to inheriting multiple times and to name clashes So what is the situation now As usual the Java designers have settled on the following pragmatic rules to deal with these problems Inheriting multiple abstract methods with the same name is not a problem they are viewed as the same method Diamond inheritance of fields one of the dificult problems is avoided because interfaces still are not allowed to contain fields that are not constants Inheriting static methods and constants which are also static by definition is not a problem because they are prefixed by the interface name when they are used so their names do not clash Inheriting from diferent interfaces multiple default methods with the same signature and diferent implementations is a problem But here Java chooses a much more pragmatic solution than some other languages instead of defining a new language construct to deal with this the compiler just reports an error In other words its your problem Java just tells you Dont do this Conclusion Interfaces are a powerful feature in Java They are useful in many situations including for defining contracts between diferent parts of the program defining types for dynamic dispatch separating the definition of a type from its implementation and allowing for multiple inheritance in Java They are very often useful in your code you should make sure you understand their behavior well The new interface features in Java 8 such as default methods are most useful when you write libraries they are less likely to be used in application code However the Java libraries now make extensive use of them so make sure you know what they do Careful use of interfaces can significantly improve the quality of your code article Michael Kölling is a Java Champion and a professor at the University of Kent England He has published two Java textbooks and numerous papers on object orientation and computing education topics and he is the lead developer of BlueJ and Greenfoot two educational programming environments Kölling is also a Distinguished Educator of the ACM There is little reason today to use marker interfaces in Java If you are tempted look instead at using annotations
You must have JavaScript enabled to view digital editions.