Home Java javaTutorial Will Java 8 default methods break user code?

Will Java 8 default methods break user code?

Apr 19, 2023 pm 06:16 PM
java

Will Java 8 default methods break user code?

At first glance, the default method brings many new features to the Java virtual machine's instruction set. Ultimately, library developers will be able to upgrade the API without introducing compatibility issues with client code. With default methods, any class that implements a library interface automatically adapts to the default methods introduced by the interface. Once the user updates the class he implements, it is easy to override the original default method with a more meaningful method. Even better, users can call the default implementation of the interface and add business logic when overriding the method.

So far, so good. However, adding default methods when creating an interface may make Java code incompatible. This can be easily understood from the example below. Let's assume that a library requires one of its interfaces as input:

interface SimpleInput {   void foo();   void bar(); }  abstract class SimpleInputAdapter implements SimpleInput {   @Override   public void bar() {     // some default behavior ...   } }
Copy after login

Before Java 8, similar to the above method of jointly using an interface and an adapter class, it was a very common design pattern in the Java programming language. . This adapter is usually provided by the library provider to save users of the library certain operations. However, if it is provided in the form of an interface, it is similar to allowing multiple inheritance.

We further assume that a user uses the following adapter:

class MyInput extends SimpleInputAdapter {   @Override   public void foo() {     // do something ...   }   @Override   public void bar() {     super.bar();     // do something additionally ...   } }
Copy after login

With this implementation, we can finally interact with the library. Notice how we override the bar method and add additional functionality to the default implementation.

What will happen if this library is ported to Java 8? First, the library will most likely deprecate the adapter class and use the default method to provide this functionality. Ultimately, the interface looks like this:

interface SimpleInput {   void foo();   default void bar() {     // some default behavior   } }
Copy after login

Using this new interface, the user can update his code to use default methods instead of the original adapter class. The ultimate consequence of using an interface instead of an adapter class is that the class can extend other classes rather than a specific adapter. Now let's practice and transplant the MyInput class to use the default method. Since we can now extend other classes, we extend a third-party base class. We don't need to care about the role of this basic class here, we can assume that this is meaningful for our function.

class MyInput extends ThirdPartyBaseClass implements SimpleInput {   @Override   public void foo() {     // do something ...   }   @Override   public void bar() {     SimpleInput.super.bar();     // do something additionally ...   } }
Copy after login

In order to achieve similar functions to the original class, we use the new syntax of Java 8 to call the default method of the specified interface. At the same time, move some of the logic in our methods to the base class. At this point, you may be patting me on the shoulder and saying, this is a very good refactoring!

We have used this library quite successfully. However, maintainers need to add another interface to provide more functionality. This interface is replaced by the ComplexInput interface, which inherits from the SimpleInput interface and adds new methods. Because default methods are generally safe to add, the maintainers have overridden SimpleInput 's default method to provide a better default method. After all, this is a common thing to do with adapter classes.

interface ComplexInput extends SimpleInput {   void qux();   @Override   default void bar() {     SimpleInput.super.bar();     // so complex, we need to do more ...   } }
Copy after login

The new features brought such good results that the people maintaining ThirdPartyBaseClass also decided to rely on this library. To do this job, it implements the ComplexInput interface in ThirdPartyLibrary.

But what does this mean for the MyInput class? In order to implicitly implement the ComplexInput interface, you can inherit the ThirdPartyBaseClass class, but calling the default method of SimpleInput suddenly becomes illegal. As a result, the user's code fails to compile. This kind of call is now prohibited because Java considers it illegal to call the parent class's method from a non-direct subclass. You can only call this default method in ComplexInput, but this requires you to explicitly implement this interface in MyInput. For users of the library, this change is not expected!

What’s even more strange is that the Java runtime does not impose such restrictions. The JVM's validator allows a compiled class to call the SimpleInput::foo method, even if the class inherits the updated ThirdPartyBaseClass, thereby implicitly implementing ComplexClass. This restriction only exists in the compiler.

The above is the detailed content of Will Java 8 default methods break user code?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles