Java 8 default methods vs. non-abstract methods in abstract classes

半世苍凉 提交于 2019-11-29 03:45:56

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)

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.

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!