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
5
votes
1 answer

Call default interface method which is overridden in superclass

I have a interface and abstract class. public class TEST extends Abstract implements Inter2{ void print() { toDO(); } public static void main(String[] args) { new TEST().toDO(); } } abstract class Abstract { …
Siva Kumar
  • 1,983
  • 3
  • 14
  • 26
5
votes
5 answers

Template method design pattern using Java 8

I want to refactor template method using java 8 new default method. Say I have a flow of process define in abstract class: public abstract class FlowManager{ public void startFlow(){ phase1(); phase2(); } public abstract…
user1409534
  • 2,140
  • 4
  • 27
  • 33
5
votes
1 answer

Lambda/default methods/type erasure quirk/bug using ECJ?

Came accross this today and spent ages trying to reproduce/figure out what was happening. Can somebody explain why this happens or is this a bug with type erasure/default methods/lambda's/polymorphism? Uncommenting the default method makes it run…
vincent_
  • 53
  • 3
5
votes
1 answer

Are sub-interfaces the solution to default-method conflicts?

Consider the following code, which is an extraction of a real use case where LinkedList implements both List and Deque. One can observe that both interfaces have a size() and an isEmpty() method, where the isEmpty() method could be made…
skiwi
  • 66,971
  • 31
  • 131
  • 216
5
votes
2 answers

Using the new Java8 interface with default methods

I have a few questions about the 'new' interfaces in Java 8, I have the following code: public interface Drawable { public void compileProgram(); public Program getProgram(); public boolean isTessellated(); public boolean…
skiwi
  • 66,971
  • 31
  • 131
  • 216
4
votes
0 answers

Calling interface's default method when superclass has private method

Consider the code below. interface I { default Number f() { return 0; } } class A { private Number f() { // if Number is replaced with other type, all will work fine return 1; } } class B extends A implements I…
4
votes
2 answers

Would it be possible to add default methods to Comparable without breaking Java?

I was thinking of proposing a feature request to add default methods called: default boolean greaterThan(T o) { return compareTo(o) > 0; } default boolean smallerThan(T o) { return compareTo(o) < 0; } default boolean atLeast(T o) { …
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
4
votes
0 answers

Explicitly calling a default method in Groovy

This question is very like the other one discussing Explicitly calling a default method in Java, but in Groovy code(ver. 2.4.16) public interface Interface { // a Java interface default void call() {} } public class Test implements Interface {…
Jay
  • 326
  • 2
  • 8
4
votes
1 answer

Java anonymous inner class calling enclosing type super types method

I give you an example to set some context here, so I have two interfaces each inheriting the same parent interface and defining their own implementation of the parent interface's abstract method. interface A { Set a(); } interface B extends…
4
votes
2 answers

Explicitly calling a default method in java - when the implemented interface uses generics

This questions is the same as this one with a twist. I have an interface such as : @Repository public interface InvoiceRepository extends JpaRepository{ // some other methods here default Invoice save(Invoice invoice) { …
Orkun
  • 6,998
  • 8
  • 56
  • 103
4
votes
1 answer

Jackson @JsonIgnore an inherited Java 8 default method

I have an interface with a default method: public interface Book { String getTitle(); default String getSentenceForTitle() { return "This book's title is " + getTitle(); } } ... and I have an JPA @Entity implementing this…
Abdull
  • 26,371
  • 26
  • 130
  • 172
4
votes
3 answers

How to call default interface method from another subclass?

Consider the following: //Fooable.java public interface Fooable { public default void foo() { System.out.println("Fooable::foo"); } //Lots of other non-default methods... } //MyFooable.java public class MyFooable implements…
Sina Madani
  • 1,246
  • 3
  • 15
  • 27
4
votes
1 answer

Using default methods in lambda expression

According to JLS 15.27.2 the lambda body has the same scope as the surrounding context and I would like to know if there is a specific reason to why default methods from the interface that is implemented by the lambda aren't available within the…
Christian Beikov
  • 15,141
  • 2
  • 32
  • 58
4
votes
1 answer

When putting a watch on a breakpoint, Java debugger shows a boxed Integer instead of primitive int

This simple example demonstrates the problem: public class Main { interface Person { default int amountOfHands() { return 2; } } public static class BasicPerson implements Person { int…
gvlasov
  • 18,638
  • 21
  • 74
  • 110
4
votes
2 answers

Fundamental difference between interface and abstract class in Java 8

Considering that interfaces now can give an implementation to the methods it provides, I cannot properly rationalize the difference between interfaces and abstract classes. Does anyone know how to explain the difference properly? I was also told…