When to use: Java 8+ interface default method, vs. abstract method

java 8 features
java 8 vs java 11
java 8 interview questions
java 8 lts
java 8 vs java 12
java 8 release date
java 8 features dzone
java 8 vs java 11 performance

Java 8 allows for default implementation of methods in interfaces called Default Methods.

I am confused between when would I use that sort of interface default method, instead of an abstract class (with abstract method(s)).

So when should interface with default methods be used and when should an abstract class (with abstract method(s)) be used? Are the abstract classes still useful in that scenario?

There's a lot more to abstract classes than default method implementations (such as private state), but as of Java 8, whenever you have the choice of either, you should go with the defender (aka. default) method in the interface.

The constraint on the default method is that it can be implemented only in the terms of calls to other interface methods, with no reference to a particular implementation's state. So the main use case is higher-level and convenience methods.

The good thing about this new feature is that, where before you were forced to use an abstract class for the convenience methods, thus constraining the implementor to single inheritance, now you can have a really clean design with just the interface and a minimum of implementation effort forced on the programmer.

The original motivation to introduce default methods to Java 8 was the desire to extend the Collections Framework interfaces with lambda-oriented methods without breaking any existing implementations. Although this is more relevant to the authors of public libraries, you may find the same feature useful in your project as well. You've got one centralized place where to add new convenience and you don't have to rely on how the rest of the type hierarchy looks.

When Will Java 11 Replace Java 8 as the Default Java?, At the moment, Java 8 and Java 11 are the LTS (long-term support) versions. for Java 8 is scheduled for January of 2019 for commercial use. If your project depends on Jack, you should migrate to using Java 8 support built into Android Studio’s default toolchain. Using the default toolchain also includes support for third-party libraries that use Java 8 language features and support for tools that depend on intermediate .class files.

There are a few technical differences. Abstract classes can still do more in comparison to Java 8 interfaces:

  1. Abstract class can have a constructor.
  2. Abstract classes are more structured and can hold a state.

Conceptually, main purpose of defender methods is a backward compatibility after introduction of new features (as lambda-functions) in Java 8.

Information about Java 8, Java 8 is the latest release for Java that contains new features, enhancements Some application providers might require that you use a particular Java release  Java 8 provides ZonedDateTime when we need to deal with time zone specific date and time. The ZoneId is an identifier used to represent different zones. There are about 40 different time zones and the ZoneId are used to represent them as follows. In this code snippet we create a Zone for Paris:

This is being described in this article. Think about forEach of Collections.

List<?> list = …

The forEach isn’t declared by java.util.List nor the java.util.Collection interface yet. One obvious solution would be to just add the new method to the existing interface and provide the implementation where required in the JDK. However, once published, it is impossible to add methods to an interface without breaking the existing implementation.

The benefit that default methods bring is that now it’s possible to add a new default method to the interface and it doesn’t break the implementations.

20 Reasons to Move On from Java 8, Java 8 introduced lambda expressions, which offer a simple syntax to create and use lambdas. Let's see an example of how this can improve  From Java 8 we can achieve something similar using Streams. Java 8 has introduced the java.util.stream which allows users to process sequence of elements. So what does one mean by processing of elements? By processing means performing operations like filter, map, limit, reduce, find, match etc. Streams and Collections-

These two are quite different:

Default methods are to add external functionality to existing classes without changing their state.

And abstract classes are a normal type of inheritance, they are normal classes which are intended to be extended.

A Tour of the Major Changes and Improvements in Java 8, This document summarizes features and enhancements in Java SE 8 and in the same annotation type more than once to the same declaration or type use. While using map() method, therefore, we need to add an extra call to retrieve the value before using the transformed value. This way, the Optional wrapper will be removed. This operation is performed implicitly when using flatMap. 13. Chaining Optionals in Java 8

As described in this article,

Abstract classes versus interfaces in Java 8

After introducing Default Method, it seems that interfaces and abstract classes are same. However, they are still different concept in Java 8.

Abstract class can define constructor. They are more structured and can have a state associated with them. While in contrast, default method can be implemented only in the terms of invoking other interface methods, with no reference to a particular implementation's state. Hence, both use for different purposes and choosing between two really depends on the scenario context.

What's New in JDK 8, This Java 8 tutorial list down important Java 8 features with examples which is a single parameter, if its type is inferred, it is not mandatory to use parentheses. This tutorial explains how to use Java 8’s predefined collector returned by Collectors.mapping() method with examples. It first explains the definition of the static mapping() method, followed by a quick explanation of its working, and then shows how to use Collector returned by Collectors.mapping() using two Java 8 code examples.

Java 8 Tutorial - Java 8 Features, There are no runtime benefits of using lambda expressions, so I will use it cautiously because I don't mind writing a few extra lines of code. A new package java.util  To use Java 8 language features when developing with the N Developer Preview, you need to use the Jack compiler. The New Project Wizard [File→ New→ Project] generates the correct configurations for projects targeting the N.

Java 8 Features with Examples, Using lambda expressions is easy. All you need to do is use a comma-separated list of parameters or collection, and use the symbol “->” within  Until Java 8, this was not possible because an interface could contain only abstract methods. However, in Java 8 you can create default methods which provide a default implementation. Thus a functional interface can contain one or more default methods, but can contain only one abstract method.

The top 5 Java 8 features for developers, Java 8 Method reference is used to refer method of functional interface . It is compact and easy form of lambda expression. Each time when you are using  Hi, I am using java8 , maven 3.5.2 version and using below configuration in pom.xml for compile java. org.apache.maven.plugins maven-compiler-plugin 3.8.0. 1.8 1.8. Question: Showing two versions like 49, 52 when I check java major version using javap. Requesting you to suggest me what could be the wrong? Regards, Munivelu.

  • Maybe you still cannot have fields, private methods, etc. in interfaces, while you can in abstract class?
  • I was wondering about this topic before, now i`m clear.Thanks to @Narendra Pathai . I would like to add link of another thread asked by you regarding same topic, as both of these were my doubts. stackoverflow.com/questions/19998309/…
  • You can find a nice post on this one here: blog.codefx.org/java/everything-about-default-methods
  • You can still sometimes code a base class as an interface even if the base class has state. It's just that the interface has to define setters and getters for the state and the concrete classes have to implement them and define the field. One restriction on this is that in an abstract class, the bean property can be private or protected. In interfaces only have public methods. So one reason you would use an abstract base class is if your classes have a property that needs to be private or protected.
  • @DaBlick Could you not solve the state problem in an interface via a HashMap. Ex: if you want a class Foo which holds int a, b, String c. and you want them to have state, create a HashMap< /*name of Foo object*/ String, /*map of fields*/ Hashmap< /*name specific Field*/ String, /*field value*/ Object>> map. When you want to "instantiate" the theoretical class Foo, you have method, instantiate(String nameOfFoo) which does map.put(nameOfFoo, fields) where fields is a HashMap<String, Object> fields.put("a", new int("5")); fields.put("b", new int("6")); fields.put("c", "blah"));
  • By this reasoning, the next thing they would add is default method declarations. I am still unsure about this, the feature seems more like a hack to me that is being exposed to everyone for misuse.
  • The only use of Abstract Classes in Java 8 era that i can see is for defining non-final fields. In Interfaces the fields are by default final so you cannot change them once they are assigned.
  • @Anuroop Not just by default---that's the only option. Interfaces cannot declare instance state, which is why abstract classes are here to stay.
  • @PhilipRego Abstract methods don't call anything because they have no implementation. The implemented methods in a class can access the class's state (instance variables). Interfaces cannot declare them so the default methods cannot access them. They have to rely on the class providing an implemented method that accesses the state.
  • Marko Topolnik, your answer is dead on. But I would like to recommend an update to your answer. You may want to add that the beauty of default methods is that, if the interface adds new default methods, your previous implementation of that interface will not break. This wasn't true prior to Java 8.
  • This answer is actually correct and makes sense especially "Conceptually, main purpose of defender methods is a backward compatibility "
  • @UnKnown this page gives more insight: docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html