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

Why many methods in JCF interfaces not made default in Java 8?

Minimum complete definition of Collection interface consists only of two methods: iterator() and size(), which are abstract in AbstractCollection. Why all other methods not made default in Java 8? Compatibility shouldn't be an issue, for example,…
leventov
  • 14,760
  • 11
  • 69
  • 98
3
votes
2 answers

C# Code reuse ( Default Interface Methods / Extensions )

I would like to ask for an advise. I am learning C# and I am always trying to reduce code duplication and I am struggling to find "best practice" in C#. I am trying to use Default interface methods to implement as much reusable code as possible,…
walter33
  • 776
  • 5
  • 12
3
votes
1 answer

Java default methods with lambda

I am learning the Java 8 syntax and came across a piece of code in our application below in an interface: default EmployeeEnricher employeeEnricher() { return builder -> { return; }; } Can someone please help me understand…
user1318369
  • 715
  • 5
  • 15
  • 34
3
votes
2 answers

Significance of inheriting method from superclass instead of default method from implementing interface in java 8

I came across following paragraph while reading about java 8 default methods from here: If any class in the hierarchy has a method with same signature, then default methods become irrelevant. A default method cannot override a method from…
MsA
  • 2,599
  • 3
  • 22
  • 47
3
votes
4 answers

Call Method from default method in java interface

This may be silly question. but i want to know there is some possibility to do this. Suppose I have interface like public interface GroupIdentifier { Integer getRevision(); } And I need another method named as getNextRevision. So what i can…
Chamly Idunil
  • 1,848
  • 1
  • 18
  • 33
3
votes
3 answers

Changing visibility of java 8 default method

I have a class (Say FOO) and it has a method with default visibility, like below : void sayHi() {} Now, If a override this method in extending class I cannot decrease it's visibility. So I can only use default or public. public class MyClassTest…
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
3
votes
1 answer

Java 8: Automatically composite default methods of multiple interfaces

I have class implements multiple interfaces which have a same default default method. I am wondering how can I composite the default method from all the interfaces. For example: interface IA { default void doA() {} default void process()…
Yiqing Zhu
  • 31
  • 3
3
votes
4 answers

Rules for Diamond prob resolution or Multiple Inheritance in Java8

In Java a class can extend only one parent class but can implement multiple interfaces. With the introduction of default methods in Java 8 interface, there’s the possibility of a class inheriting more than one method with the same signature by…
Ankur Singh
  • 339
  • 3
  • 16
3
votes
3 answers

Java 8 doesn't provide the same solution to allow multiple inheritance which they gave to solve interface default methods

Problem: We know that Java doesn’t allow to extend multiple classes because it would result in the Diamond Problem where the compiler could’t decide which superclass method to use. With interface default methods the Diamond Problem were introduction…
3
votes
1 answer

Why an inherited default-method cannot implement another interface like an inherited class method?

I'm studying Java 8, and I have encountered a behavior with default methods I cannot understand completely. First, an "old-school" Java snippet which compiles and runs perfectly: abstract class A { public void print() { …
1d0m3n30
  • 430
  • 6
  • 12
3
votes
2 answers

java8 Interface allowing public default method

In java 8 default method implementation can take both public and default modifier. What is the main difference between below two methods. Under which conditions which type need to follow. default int makeMul(int x, int y) { return x *…
pokuri
  • 156
  • 7
3
votes
2 answers

Invoke non-abstract method of super interface in concrete class

The Calculator interface has calculate abstract method and ramdom() non-abstract method. I want to use super ramdom() and also Override ramdom() at concrete class CalculatorImpl. My question is why I have to call like that Calculator.super.ramdom()…
Mizuki
  • 2,153
  • 1
  • 17
  • 29
3
votes
1 answer

Java: invoke a default method in another default method of the same interface

I'm very new to the java 8 features and try to understand default methods. Is there an easier way to invoke a default method by another default method of the same interface than using an anonymous class? For example: public class Frame{ public…
3
votes
1 answer

Why doesn't Delayed provide a default method for compareTo?

The interface Delayed requires any implementation of this interface [to] define a compareTo method that provides an ordering consistent with its getDelay method. However I'm wondering, why there isn't a default implementation in Java 8, as…
Sebastian S
  • 4,420
  • 4
  • 34
  • 63
3
votes
1 answer

Java8 overriding (extending) default method

Suppose we have a default method in a interface, in implementing class if we need to add some additional logic besides the one the default method already does we have to copy the whole method? is there any possibility to reuse default method... like…
vach
  • 10,571
  • 12
  • 68
  • 106