Traps brought about by the default implementation of the Java 8 interface

Let's stop using the EOL version ...

It's been a while since I implemented Iterator, which may be the first time. When I commit, it turns red in other people's Eclipse for some reason.

Java8Iterator.java


import java.util.Iterator;

public class Java8Iterator implements Iterator<Integer> {

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Integer next() {
		// TODO Auto-generated method stub
		return null;
	}

}

Java7Iterator.java


import java.util.Iterator;

public class Java7Iterator implements Iterator<Integer> {

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Integer next() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void remove() {
		// TODO Auto-generated method stub
		
	}

}

Can you see the difference? Java7Iterator has a remove method, and Java8Iterator does not implement the remove method. There are environments where there is no problem even if it is not implemented, and there are environments where compilation errors occur if it is not implemented.

Looking at the Java Build Path of the project, it is "JRE System Library [Java SE-1.6]". Wasn't it even Java7!

image.png

If the project settings are Java 1.6 but jre 1.8.0 is used, the code generated by Eclipse's "Add unimplemented methods" does not include the remove method. When I configured the Compatible JRE to use jre 1.6 properly, the generated code now includes a remove method.

If you check the src.zip of JDK8, the default implementation is described in the Iterator interface. If there is a default implementation, it will not be treated as an unimplemented method.

That's why you should either bury an EOL version of Java or set up a tightly compatible JRE.

Recommended Posts

Traps brought about by the default implementation of the Java 8 interface
Refactor the Decorator pattern implementation with a functional interface
Commentary: About the interface
Traps brought about by the default implementation of the Java 8 interface
[Java] Functional interface / lambda expression
Until the code is executed
[Java] I thought about the merits and uses of "interface"
About Java interface
About interface, java interface
About the description order of Java system properties
About Java functional interface
Commentary: About the interface
About CLDR locale data enabled by default from Java 9
Credentials referenced by the AWS SDK for Java by default
Are you using the default method of the interface properly?
About the handling of Null
About an instance of java
[Java] Coverage report could not be created with the combination of default method of Cobertura + interface
About the description of Docker-compose.yml
[Java] Implementation of Faistel Network
[Java] Simplify the implementation of data history management with Reladomo
[For beginners] About the JavaScript syntax explained by Java Gold
About the operation of Java reading party BOF-held monthly since 1998
Implementation of gzip in java
Summary of values returned by the Spliterator characteristics method #java
About the default behavior of decimal point rounding using java.text.NumberFormat
Implementation of tri-tree in Java
When I was worried about static methods in java interface, I arrived in the order of name interpretation
How to use the function implemented in Kotlin Interface introduced in Maven as the default implementation from Java 8
Let's talk about the passing experience of Java SE 8 Programmer II
Switch the version of java installed by SDKMAN when moving directories
The story of not knowing the behavior of String by passing Java by reference
A story about hitting the League Of Legends API with JAVA
[Java] Appropriate introduction by the people of Tempa Java Part 0 (Code rules)
About the behavior of ruby Hash # ==
Access the network interface in Java
[Java] Delete the elements of List
About the basics of Android development
[Java version] The story of serialization
About Lambda, Stream, LocalDate of Java8
[Java beginner] About abstraction and interface
[Rails] About implementation of like function
[Java] Output by FormatStyle of DateTimeFormatter
About the current development environment (Java 8)
About the role of the initialize method
Think about the 7 rules of Optional
Default implementation of Object.equals () and Object.hashCode ()
The origin of Java lambda expressions
Implementation of like function in Java
Summary about the introduction of Device
About the log level of java.util.logging.Logger
About the classification and concept of Immutable / Mutable / Const / Variable of Java and Kotlin.
This and that of the implementation of date judgment within the period in Java
Check the domain by checking the MX record of the email address with java
Implementation of clone method for Java Record
[Java Siler] About type inference by var
Implementation of DBlayer in Java (RDB, MySQL)
Get the result of POST in Java
Significance of interface learned from Java Collection
About the version of Docker's Node.js image
Guess about the 2017 Java Persistence Framework (3) Reladomo
Check the contents of the Java certificate store
Examine the memory usage of Java elements
What is testing? ・ About the importance of testing
[Java] Get the day of the specific day of the week