0

When I see the Iterable interface source, it looks like the foreach method and Spliterator methods are not abstract. How an interface can have non abstract methods? Or is there anything I am missing in this? See the Iterbale interface source below.

package java.lang; import java.util.Iterator; import java.util.Objects; import java.util.Spliterator; import java.util.Spliterators; import java.util.function.Consumer; public abstract interface Iterable<T> { public abstract Iterator<T> iterator(); public void forEach(Consumer<? super T> paramConsumer) { Objects.requireNonNull(paramConsumer); Iterator localIterator = iterator(); while (localIterator.hasNext()) { Object localObject = localIterator.next(); paramConsumer.accept(localObject); } } public Spliterator<T> spliterator() { return Spliterators.spliteratorUnknownSize(iterator(), 0); } } /* Location: C:\Program Files\Java\jre1.8.0_121\lib\rt.jar * Qualified Name: java.lang.Iterable * Java Class Version: 8 (52.0) * JD-Core Version: 0.7.1 */ 
6
  • Yes, Java 8 interfaces can have default methods. Read the Java tutorial, or the myriad of blog posts and articles that have been written since before Java 8 came out. Commented Mar 24, 2017 at 12:37
  • This doesn't look like the actual source code. forEach should be declared as default. And why is it declared as abstract interface? What version are you looking at? Commented Mar 24, 2017 at 12:39
  • @Tom It is Java Class Version: 8 (52.0) Commented Mar 24, 2017 at 12:52
  • No it isn't. It doesn't look like that. Unless you used a decompiler instead of looking at the actual source. Commented Mar 24, 2017 at 12:53
  • Neither the class from src.zip nor the decompiled version (using Fernflower decompiler) looks like that. Commented Mar 24, 2017 at 12:59

1 Answer 1

2

With Java 8 you can define a default implementation in the interface.
It is what java.lang.Iterable does :

public interface Iterable<T> { ... default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } ... } 

Your actual code doesn't refer the Java 8 source code.

Sign up to request clarification or add additional context in comments.

4 Comments

My code refers to java 8 source. /* Location: C:\Program Files\Java\jre1.8.0_121\lib\rt.jar * Qualified Name: java.lang.Iterable * Java Class Version: 8 (52.0) * JD-Core Version: 0.7.1 */
It is a viewable version of the compiled class not the source code of the class. Here is the source code : grepcode.com/file_/repository.grepcode.com/java/root/jdk/…
I am seeing the code in eclipse, may be that's why it is showing the compiled version.
If Eclipse shows the source code in this way, it means Eclipse doesn't not know where the source code is. In the preferences->installed JREs, edit the JDK/JRE you are using. You should have the source attachement button to specify where the code source is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.