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
8
votes
3 answers

Getter in an interface with default method JSF

I have an interface with the following default method: default Integer getCurrentYear() {return DateUtil.getYear();} I also have a controller that implements this interface, but it does not overwrite the method. public class NotifyController…
8
votes
1 answer

Java debugger can't call some default method implementations

I'm coding in IntelliJ IDEA. When debugging my application, I can't use some default method implementations in Watches. Here is a condensed example: public class Friendship { interface Friend { default void sayHiTo(Friend friend) { …
gvlasov
  • 18,638
  • 21
  • 74
  • 110
8
votes
4 answers

Java 8 default method readability

Java 8 introduces the concept of default methods. Consider the following interface with a default method : public interface IDefaultMethod { public abstract void musImplementThisMethod(); public default void mayOrMayNotImplementThisMethod()…
Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
7
votes
4 answers

Interface Segregation Principle and default methods in Java 8

As per the Interface Segregation Principle clients should not be forced to implement the unwanted methods of an interface ...and so we should define interfaces to have logical separation. But default methods introduced in Java 8 have provided the…
7
votes
2 answers

Open Closed Principle Vs Default Implementation

Java 8 introduced the concept of default implementation for interfaces? Isn't this violating Open Closed Principle, since based on the example on https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html, it seems like you can always…
7
votes
3 answers

Java8 slow compiling for interfaces with thousands of default methods with the same name

given the interfaces (which are very large and generated out of language definitions): interface VisitorA { default void visit(ASTA1 node) {...} ... default void visit(ASTA2000 node) {...} } interface VisitorB extends VisitorA { default…
7
votes
3 answers

Designing interface for hierarchical entity

I have to design an interface for hierarchical entity: interface HierarchicalEntity> { T getParent(); Stream getAncestors(); } It's quite easy to implement default getAncestors() method in terms of…
Aliaxander
  • 2,547
  • 4
  • 20
  • 45
7
votes
1 answer

Why are certain Object methods not callable from default methods?

When creating a default method in Java 8, certain Object methods are not callable from within the default method. For example: interface I { default void m() { this.toString(); // works. this.clone(); // compile-time error, "The…
Raffi Khatchadourian
  • 3,042
  • 3
  • 31
  • 37
7
votes
2 answers

Shadowing default method of an interface

Consider the following case, interface IFace1 { default void printHello() { System.out.println("IFace1"); } } interface IFace2 { void printHello(); } public class Test implements IFace1, IFace2 { public static void…
akash
  • 22,664
  • 11
  • 59
  • 87
7
votes
4 answers

default methods in interface but only static final fields

I understand that all fields in an Inteface is implicitly static and final. And this made sense before Java 8. But with the introduction of default methods, interfaces also have all the capabilities of an abstract class. And hence non-static and…
Codebender
  • 14,221
  • 7
  • 48
  • 85
7
votes
2 answers

What was the design consideration of not allowing use-site injection of extension methods to java 8?

We have default methods that were also referred to as defender methods and 'virtual extension methods'. While I appreciate the tremendous value of default methods (that in some aspects are even more powerful than their C# counterparts), I wonder…
Vitaliy
  • 8,044
  • 7
  • 38
  • 66
7
votes
1 answer

Why didn't Java 8 add `withLock` default methods to the `java.util.concurrent.locks.Lock` interface?

Along the same lines as this question, I wonder why the Java team didn't add a few default methods to the Lock interface, something like this: public default void withLock(Runnable r) { lock(); try { r.run(); } finally { unlock(); …
rxg
  • 3,777
  • 22
  • 42
6
votes
2 answers

Kotlin enforce implementing class to be a supertype of another type

Since multiple inheritance is not allowed in java/kotlin, it's usefull to take advantage of interface default methods. Given example: abstract class Animal { fun beAnimal() { println("I'm animal!") } } abstract class Mammal :…
xinaiz
  • 7,744
  • 6
  • 34
  • 78
6
votes
2 answers

Throw exception in interface default method

Recently I came across this code. public interface CustomerQueryService { default Customer getCustomerById(long id) { throw new NotImplementedException(); } } Later, it turned out that it's general convention for this…
k13i
  • 4,011
  • 3
  • 35
  • 63
6
votes
1 answer

Are default methods in java interfaces an anti-pattern?

Java 8 introduced default methods on interfaces to provide backwards compatibility for implementations of the collections interfaces, to avoid MethodNotFound errors on legacy libraries. i.e A library with a java 7 implementation of List will not…
murungu
  • 2,090
  • 4
  • 21
  • 45