- 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.
- PyTorch自然語言處理入門與實(shí)戰(zhàn)
- SharePoint Development with the SharePoint Framework
- Learning DHTMLX Suite UI
- 軟件品質(zhì)之完美管理:實(shí)戰(zhàn)經(jīng)典
- PHP+Ajax+jQuery網(wǎng)站開發(fā)項(xiàng)目式教程
- INSTANT Yii 1.1 Application Development Starter
- Geospatial Development By Example with Python
- Web前端開發(fā)技術(shù):HTML、CSS、JavaScript
- TypeScript圖形渲染實(shí)戰(zhàn):2D架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)
- Android高級開發(fā)實(shí)戰(zhàn):UI、NDK與安全
- 3D Printing Designs:The Sun Puzzle
- Android應(yīng)用程序設(shè)計(jì)
- Puppet 5 Beginner's Guide(Third Edition)
- Less Web Development Cookbook
- 現(xiàn)代C++語言核心特性解析