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
629
votes
16 answers

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

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…
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
374
votes
5 answers

Why is "final" not allowed in Java 8 interface methods?

One of the most useful features of Java 8 are the new default methods on interfaces. There are essentially two reasons (there may be others) why they have been introduced: Providing actual default implementations. Example:…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
341
votes
7 answers

Explicitly calling a default method in Java

Java 8 introduces default methods to provide the ability to extend interfaces without the need to modify existing implementations. I wonder if it's possible to explicitly invoke the default implementation of a method when that method has been…
GOTO 0
  • 42,323
  • 22
  • 125
  • 158
223
votes
2 answers

What is the reason why “synchronized” is not allowed in Java 8 interface methods?

In Java 8, I can easily write: interface Interface1 { default void method1() { synchronized (this) { // Something } } static void method2() { synchronized (Interface1.class) { //…
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
162
votes
5 answers

Java8: Why is it forbidden to define a default method for a method from java.lang.Object

Default methods are a nice new tool in our Java toolbox. However, I tried to write an interface that defines a default version of the toString method. Java tells me that this is forbidden, since methods declared in java.lang.Object may not be…
gexicide
  • 38,535
  • 21
  • 92
  • 152
120
votes
1 answer

Java 8 default methods as traits : safe?

Is it a safe practice to use default methods as a poor's man version of traits in Java 8? Some claim it may make pandas sad if you use them just for the sake of it, because it's cool, but that's not my intention. It is also often reminded that…
youri
  • 3,685
  • 5
  • 23
  • 43
96
votes
4 answers

When is an interface with a default method initialized?

While searching through the Java Language Specification to answer this question, I learned that Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by the class are not initialized. Similarly, the…
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
56
votes
5 answers

Do Java 8 default methods break source compatibility?

It has generally been the case the Java source code has been forward compatible. Until Java 8, as far as I know, both compiled classes and source have been forward compatible with later JDK/JVM releases. [Update: this is not correct, see comments…
Paul
  • 3,009
  • 16
  • 33
50
votes
5 answers

Why can we not use default methods in lambda expressions?

I was reading this tutorial on Java 8 where the writer showed the code: interface Formula { double calculate(int a); default double sqrt(int a) { return Math.sqrt(a); } } And then said Default methods cannot be accessed from…
codegasmer
  • 1,462
  • 1
  • 15
  • 47
48
votes
5 answers

Purpose of Default or Defender methods in Java 8

Java 8 has included a new feature called Defender methods which allows creation of default method implementation in interface. Now first of all this is a huge paradigm shift for all condensed programmers in Java. I viewed a JavaOne 13 presentation…
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
42
votes
1 answer

Calling default method in interface when having conflict with private method

Consider below class hierarchy. class ClassA { private void hello() { System.out.println("Hello from A"); } } interface Myinterface { default void hello() { System.out.println("Hello from Interface"); } } class…
Shailesh Pratapwar
  • 4,054
  • 3
  • 35
  • 46
41
votes
5 answers

Java 8 add extension/default method to class

I am looking for a java equivalent to the C# extension methods feature. Now I have been reading about Java 8's default methods, but as far as I can see, I can only add these to interfaces... ...is there any language feature that will allow me to…
Cheetah
  • 13,785
  • 31
  • 106
  • 190
37
votes
8 answers

How to explicitly invoke default method from a dynamic Proxy?

Since Java 8 interfaces could have default methods. I know how to invoke the method explicitly from the implementing method, i.e. (see Explicitly calling a default method in Java) But how do I explicitly invoke the default method using reflection…
Gerald Mücke
  • 10,724
  • 2
  • 50
  • 67
35
votes
1 answer

Why does Java 8 not allow non-public default methods?

Let's take an example: public interface Testerface { default public String example() { return "Hello"; } } public class Tester implements Testerface { @Override public String example() { return…
Rogue
  • 11,105
  • 5
  • 45
  • 71
32
votes
1 answer

Default methods and interfaces extending other interfaces

Suppose there are two interfaces Interface1 and Interface2 where Interface2 extends Interface1. interface Interface1 { default void method() { System.out.println("1"); } // Other methods } interface Interface2 extends…
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
1
2 3
11 12