- Java 9 Programming Blueprints
- Jason Lee
- 462字
- 2021-07-02 18:56:26
Default methods
Before turning our attention to Java 9, let's take a look at one more significant language feature: default methods. Since the beginning of Java, an interface was used to define how a class looks, implying a certain type of behavior, but was unable to implement that behavior. This made polymorphism much simpler in a lot of cases, as any number of classes could implement a given interface, and the consuming code treats them as that interface, rather than whatever concrete class they actually are.
One of the problems that have confronted API developers over the years, though, was how to evolve an API and its interfaces without breaking existing code. For example, take the ActionSource interface from the JavaServer Faces 1.1 specification. When the JSF 1.2 expert group was working on the next revision of the specification, they identified the need to add a new property to the interface, which would result in two new methods--the getters and setters. They could not simply add the methods to the interface, as that would break every implementation of the specification, requiring the maintainers of the implementation to update their classes. Obviously, this sort of breakage is unacceptable, so JSF 1.2 introduced ActionSource2, which extends ActionSource and adds the new methods. While this approach is considered ugly by many, the 1.2 expert group had a few choices, and none of them were very good.
With Java 8, though, interfaces can now specify a default method on the interface definition, which the compiler will use for the method implementation if the extending class does not provide one. Let's take the following piece of code as an example:
public interface Speaker { void saySomething(String message); } public class SpeakerImpl implements Speaker { public void saySomething(String message) { System.out.println(message); } }
We've developed our API and made it available to the public, and it's proved to be really popular. Over time, though, we've identified an improvement we'd like to make: we'd like to add some convenience methods, such as sayHello() and sayGoodbye(), to save our users a little time. However, as discussed earlier, if we just add these new methods to the interface, we'll break our users' code as soon as they update to the new version of the library. Default methods allow us to extend the interface and avoid the breakage by defining an implementation:
public interface Speaker { void saySomething(String message); default public void sayHello() { System.out.println("Hello"); } default public void sayGoodbye() { System.out.println("Good bye"); } }
Now, when users update their library JARs, they immediately gain these new methods and their behavior, without making any changes. Of course, to use these methods, the users will need to modify their code, but they need not do so until--and if--they want to.
- 玩轉(zhuǎn)Scratch少兒趣味編程
- Apache Spark 2 for Beginners
- 實戰(zhàn)Java程序設計
- TypeScript實戰(zhàn)指南
- Gradle for Android
- OpenStack Orchestration
- 利用Python進行數(shù)據(jù)分析
- 細說Python編程:從入門到科學計算
- Python 3 數(shù)據(jù)分析與機器學習實戰(zhàn)
- 從Power BI到Analysis Services:企業(yè)級數(shù)據(jù)分析實戰(zhàn)
- Java Web應用開發(fā)給力起飛
- Unity Android Game Development by Example Beginner's Guide
- 大數(shù)據(jù)時代的企業(yè)升級之道(全3冊)
- 程序員的成長課
- 軟件測試技術(shù)