Questions tagged [default-method]

A default method is a feature introduced in Java 8 which allows an interface to declare a method body. Classes which implement the interface are not required to override a default method. Use this tag for questions relating to default methods.

A default method is a feature introduced in which allows an interface to declare a method body. Classes which implement the interface are not required to override a default method. An interface method is made default by adding the default keyword, also introduced in Java 8.

In the following example, then method is a default method of the Command interface.

@FunctionalInterface
interface Command {

    void execute();

    default Command then(Command next) {
        return () -> {
            this.execute();
            next.execute();
        };
    }

}

Adding a default method to an interface or changing a method from abstract to default does not break compatibility with a pre-existing binary if the binary does not attempt to invoke the method.

See also:

168 questions
12
votes
4 answers

static method in class have same signature as default method in interface

I have below scenario : class C { static void m1() {} } interface I { default void m1() {} } //this will give compilation error : inherited method from C cannot hide public abstract method in I class Main extends C implements I { } Below…
12
votes
5 answers

Java default interface methods concrete use cases

Java 9 is near to come and more features will be added to Java interfaces, like private methods. default methods in interfaces were added in Java 8, essentially to support the use of lambdas inside collections without breaking retro-compatibility…
riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
12
votes
2 answers

Why no default clone() in Cloneable in Java 8

Cloneable in Java is inherently broken. Specifically, my biggest problem with the interface is it expects a method behavior that doesn't define the method itself. So if traversing through a Cloneable list you must use reflection to access its…
danthonywalker
  • 368
  • 3
  • 14
12
votes
5 answers

is there a way to add a default constructor to an interface

With default methods now added to Java 8, is there any way to create a default constructor? I've tried: public interface KadContent { public default KadContent() { } ... Getting the error expected from Netbeans Why…
Joshua Kissoon
  • 3,269
  • 6
  • 32
  • 58
11
votes
3 answers

Default interface method for abstract superclass

Lets say I have the following structure: abstract class A { abstract boolean foo(); } interface B { default boolean foo() { return doBlah(); } } class C extends A implements B { //function foo } Java will now complain that class C…
L0laapk3
  • 890
  • 10
  • 26
11
votes
3 answers

Adding methods or not adding methods to interface?

In Java 8, we can have default implementations for methods in interfaces, in addition to declarations which need to be implemented in the concrete classes. Is it a good design or best practice to have default methods in an interface, or did Java 8…
Kathiresa
  • 421
  • 2
  • 6
  • 15
11
votes
5 answers

Inheritance, composition and default methods

It is usually admitted that extending implementations of an interface through inheritance is not best practice, and that composition (eg. implementing the interface again from scratch) is more maintenable. This works because the interface contract…
Ulysse Mizrahi
  • 681
  • 7
  • 19
11
votes
3 answers

JDiagram older version throwing StackOverflowError with JRE 8 at ExtendedArrayList.sort

I'm using JDiagram JAR like below Diagram myDigram = new Diagram(); myDigram.routeAllLinks(); This code works fine when run with JRE 7 however when it is run with JRE 8, following error is being thrown: java.lang.StackOverflowError at…
Rahul Winner
  • 430
  • 3
  • 16
11
votes
4 answers

can marker interface like serializable contain default methods?

I think it can't, because marker interface principle is to not have any methods, but since default methods are not abstract I am not sure.
10
votes
2 answers

Lombok @Slf4j and interfaces?

I am trying to add logging to my interface default method. For example: @Slf4j // not allowed interface MyIFace { default ThickAndThin doThisAndThat() { log.error("default doThisAndThat() called"); } } Problem is that I…
pirho
  • 11,565
  • 12
  • 43
  • 70
10
votes
1 answer

Strange Default Method behavior with different Java versions

Let's say I have the following class hierarchy: interface Collection { Collection $plus(E element) } interface MutableCollection extends Collection { @Override MutableCollection $plus(E element) } interface Set
10
votes
1 answer

Should we do unit testing for default methods in interfaces (Java 8)?

I feel a bit confused about the default methods implementations in interfaces introduced in Java 8. I was wondering if we should write JUnit tests specifically for an interface and its implemented methods. I tried to google it, but I couldn't find…
user998692
  • 5,172
  • 7
  • 40
  • 63
9
votes
1 answer

Visitor pattern with Java 8 default methods

Visitor pattern (double dispatch) is a very useful pattern in its own rights, but it has often been scrutinized of breaking interfaces if any new member is added to the inheritance hierarchy, which is a valid point. But after the introduction of…
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
8
votes
1 answer

Spring JPA repository interface and default methods use case

I am currently wondering whether a particular use case could be elegantly addressed by using a default interface method inside a JPA repository. Suppose we have the following entity and supporting types: public enum Status { STATUS_1, …
Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
8
votes
2 answers

Using default method raises AbstractMethorError in Android release build

I have an interface that inherits from Android's TextWatcher to only implement afterTextChanged method. I have enabled Java 8 support in my project, and added source and target compatibility options in build.gradle file, but even though it works…
1 2
3
11 12