13
votes

Java 8 interface default methods vs. non-abstract methods in abstract classes - are there any differences between the two (besides the differences of iface - class, visibility etc.)

Isn't a default method a step back in Java, meaning it's against the essence that Java has advertised for years?!

5
It's a change in direction, for sure. I wouldn't call it a step back, though.yshavit
Yeah, but the lame, verbose parts of Java (syntax, clear distinction between iface and class), while by some considered poor, is IMO the feature of Java that makes it popular, easy to reason about and safe for corpos.. And now they make a Scala out of Java.. I u want functional programming, grab a Haskell -.-Parobay
Nope. If you try, Java will force you (in the class that implements both methods) to provide your own implementation. That implementation may delegate to one of the others. I believe the syntax will be something like TheInterfaceIWant.super.theMethod(...). See lambdafaq.org/what-about-the-diamond-problemyshavit
@yshavit thank's for the link!Parobay

5 Answers

8
votes

non-abstract methods in abstract classes will be called when it's concrete subclass calls super() if it is overridden. So there are multiple possibilities. If method is not overridden then the super class method will be executed. if we use super() in the concrete subclass method then the overridden method with the super class method will be executed.

Where as Java 8 interface default methods are completely different. It provided a choice to the developers to implement the method in the implementing class or not. If the function is not implemented then and only then the default method will be executed.

Possible Use Case :

The most important use case for this new feature in the JDK libraries is the possibility to extend existing interfaces without breaking existing implementers: adding a new abstract method to an interface would require all implementing classes to implement that new method.(Source)

4
votes

The important thing to keep in mind is that default methods don't have access to state, only to behaviour. It is actually a great place to define reasonable, default, behaviour.

Imagine you have an interface:

public interface Plant {
    enum Pace { FAST, SLOW; }

    void grow(Pace pace);
    void growFast();
    void growSlow();
}

It seems reasonable to provide a default behaviour:

default void growFast() { grow(Pace.FAST); }
default void growSlow() { grow(Pace.SLOW); }

This is a simplistic example but shows how default methods can be helpful. In this case, the way growSlow or growFast behaves is part of the interface contract so it makes sense to define their behaviour at the interface level.

However the interface makes no assumption about how the action "grow a plant" is implemented. That could be defined in an abstract class.

0
votes

Firstly Default methods allow you to add new methods to interface without breaking existing implementations.

Also take an example of Collections class which is a utility class for Collection interface right.

So using default methods we can now move all the utility methods as default implementation in the Collection itself, which will make more sense than making a separate class for such utilities.

Also you will be able to inherit methods from multiple interfaces, which could not have been done using plain abstract classes.

0
votes

the big difference is that the constructor can possibly be run in an anonymous class, possibly with checked exceptions. this prevents anonymous classes being functional interfaces, and in turn makes them unavailable to use as the basis for a lambda expression.

0
votes

The main differences are: Java 8 default methods of interfaces are public, while non-abstract (concrete) methods of abstract classes can be defined as public, protected or private. Lambda expressions got introduced in Java 8, to use lambda features we need default methods(to preserve backward compatibility), non-abstract methods of abstract classes can not serve the purpose.