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

maven-plugin-plugin:descriptor goal fails at the and of file

While developing a maven plugin the build prints error: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.3:descriptor (default-descriptor) on project default-method-demo: Execution default-descriptor of goal…
czerny
  • 15,090
  • 14
  • 68
  • 96
28
votes
7 answers

Can you make mockito (1.10.17) work with default methods in interfaces?

I am a big fan of mockito, unfortunately for one of my projects which uses Java 8, it fails on me... Scenario: public final class MockTest { @Test public void testDefaultMethodsWithMocks() { final Foo foo = mock(Foo.class); …
fge
  • 119,121
  • 33
  • 254
  • 329
24
votes
2 answers

"Property not found on type" when using interface default methods in JSP EL

Consider the following interface: public interface I { default String getProperty() { return "..."; } } and the implementing class which just re-uses the default implementation: public final class C implements I { //…
Bass
  • 4,977
  • 2
  • 36
  • 82
20
votes
1 answer

Functional Interface Inheritance Quirk

I have a custom interface I've been using for some time that looks something like this: public interface Function { R call(T input); } I'd like to retrofit this interface with both Java's Function as well as Guava's Function, while…
shmosel
  • 49,289
  • 6
  • 73
  • 138
19
votes
1 answer

Java 8 default method interface override Object equals method

public interface Table { @Overrride default boolean equals(Object other) { //do something and return true/false } } Why does the above code has compilation error of "java: default method equals in interface Table overrides a…
Wins
  • 3,420
  • 4
  • 36
  • 70
18
votes
2 answers

java8: dealing with default methods

While writing a crypto utility class I ran into a problem with the following method: public static void destroy(Key key) throws DestroyFailedException { if(Destroyable.class.isInstance(key)) { ((Destroyable)key).destroy(); …
A4L
  • 17,353
  • 6
  • 49
  • 70
17
votes
3 answers

Java 8 interface default method doesn't seem to declare property

In my application I run into a problem that when a getter in a class is defaulted in an interface only (Java 8 feature), there is no Java Beans property as a result. I.e. for normal method invocation it works just as a standard method, but for…
user319799
15
votes
2 answers

Why does order of implementing Interfaces (with default methods) matter in Java 8?

As we all know, multiple interfaces can implemented in Java. Does the order of their implementation matter? I mean, is implementing B, C is same as C, B in Java 8? My tests show order does matter - but can anyone explain the logic behind…
Pradip Kharbuja
  • 3,442
  • 6
  • 29
  • 50
14
votes
3 answers

why Interface Default methods?

Learning java 8 default methods . This link like any other resource on internet says In ‘the strictest sense’, Default methods are a step backwards because they allow you to ‘pollute’ your interfaces with code. But they provide the most…
M Sach
  • 33,416
  • 76
  • 221
  • 314
14
votes
1 answer

Can you call the parent interface's default method from an interface that subclasses that interface?

In java 8 I have something like this: package test; public class SimpleFuncInterfaceTest { public static void carryOutWork(AFunctionalInterface sfi){ sfi.doWork(); } public static void main(String[] args) { …
ggb667
  • 1,881
  • 2
  • 20
  • 44
13
votes
1 answer

When two interfaces have conflicting return types, why does one method become default?

In Java 8, if I have two interfaces with different (but compatible) return types, reflection tells me that one of the two methods is a default method, even though I haven't actually declared the method as default or provided a method body. For…
schmod
  • 777
  • 9
  • 19
13
votes
3 answers

Super class method and Interface default method conflict resolution

Consider the below example, public class Testing extends SupCls implements Intf { public static void main(String[] args) { new Testing().test(); } } class SupCls { public void test() { System.out.println("From SupCls"); …
Codebender
  • 14,221
  • 7
  • 48
  • 85
13
votes
4 answers

Extending List in Java 8

I often want to map one list into another list. For example if I had a list of people, and I wanted a list of their names, I would like to do: GOAL List people = ... ; List names = people.map(x -> x.getName()); Something like this…
Alden
  • 6,553
  • 2
  • 36
  • 50
13
votes
5 answers

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

Java 8 interface default methods vs. non-abstract methods in abstract classes - are there any differences between the two (besides the differences of iface - class, visibility etc.) Isn't a default method a step back in Java, meaning it's against…
Parobay
  • 2,549
  • 3
  • 24
  • 36
12
votes
1 answer

Implementing methods using default methods of interfaces - Contradictory?

Introduction I have read multiple posts about implementing interfaces and abstract classes here on SO. I have found one in particular that I would like to link here - Link - Interface with default methods vs abstract class, it covers the same…
CrimsonMike
  • 263
  • 1
  • 13
1
2
3
11 12