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
 */
Vineesh
  • 3,762
  • 20
  • 37
  • 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. – JB Nizet Mar 24 '17 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? – Tom Mar 24 '17 at 12:39
  • @Tom It is Java Class Version: 8 (52.0) – Vineesh Mar 24 '17 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. – Kayaman Mar 24 '17 at 12:53
  • Neither the class from src.zip nor the decompiled version (using Fernflower decompiler) looks like that. – Tom Mar 24 '17 at 12:59
  • @Tom it is a decompiled version (JD decompiler), see comment at the end of the code – user85421 Mar 24 '17 at 13:05

1 Answers1

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.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • 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 */ – Vineesh Mar 24 '17 at 12:51
  • It is a viewable version of the compiled class not the source code of the class. Here is the source code : http://grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/lang/Iterable.java/?v=source – davidxxx Mar 24 '17 at 12:52
  • I am seeing the code in eclipse, may be that's why it is showing the compiled version. – Vineesh Mar 24 '17 at 12:57
  • 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. – davidxxx Mar 24 '17 at 13:01