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

Java 8 - Default methods for equals and hashcode

I have created default methods in an interface for implementing equals(Object) and hashCode() in predictable manner. I use reflection to iterate all fields in a type (class) to extract the values and compare them. The code depends on Apache Commons…
thomas77
  • 1,100
  • 13
  • 27
2
votes
1 answer

Why, after compiling an interface, "default" method modifier is gone from "javap -v"?

I have observed, that after compiling an interface containing default method definition, like: interface Delta { default void someMethod() { System.out.println("Hi."); } } and after disassembling the respective .class file…
Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66
2
votes
1 answer

Where are the default methods of interface stored in memory?

I have gone through a number of posts, but all seem to answer that where are the static methods of an interface stored. However, an interface can have abstract, static and default methods. I know about static and abstract methods. But, I am not able…
KnockingHeads
  • 1,569
  • 1
  • 22
  • 42
2
votes
1 answer

Java ignores default interface methods in an imported library and treats them as abstract

I am trying to use the Functional Java library. I just include it in build.gradle with dependencies { compile "org.functionaljava:functionaljava:4.8.1" } and compile using openjdk-15 and it just works... mostly. The problem I have is the…
Dufaer
  • 55
  • 6
2
votes
2 answers

How to use Java 8 feature of default method in interface written in Kotlin

I have a class A in java and interface B in Kotlin. // kotlin interface B { fun optional() } // java class A implements B { } I want to write the default method (Java 8 feature) in Kotlin interface (B) and want to implement it in the class…
Ajinkya S
  • 570
  • 7
  • 17
2
votes
1 answer

Why this type is not an Interface?

I want to make an interface with default implementation for equality and comparison functions. If I remove everything from the type IKeyable<'A> except the Key member, it is a valid interface, as long as I don't add a default implementation.…
Jean Lopes
  • 33
  • 3
2
votes
2 answers

Does interface provide full abstraction? How?

As much i know interface provides full abstraction because it can't have any concrete method like abstract class. But from java 8, interfaces can have concrete methods using default keyword and the classes that implement the interface can override…
SbrTa
  • 148
  • 13
2
votes
2 answers

covariant return type for default method

I can override a method with covariant return type, but is it possible to override a default method with covariant return type? In the following example, I would like to override getFirstLeg without rewriting the default method, but Java does not…
Fan
  • 315
  • 3
  • 11
2
votes
4 answers

Does Java have plan that default method (java8) Substitute for Abstract Class?

Does Java have plan that default method substitute for Abstract Class? I could not find a real case to use default method instead of Abstract?
Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141
2
votes
1 answer

Two default methods calling each other

I have an interface like this: public interface Foobar { default void foo( Bar bar ) { foo( bar, 1 ); } default void foo( Bar bar, int n ) { for ( int i = 0; i < n; i++ ) …
Androbin
  • 991
  • 10
  • 27
2
votes
2 answers

Why not default constructors in Java 8?

I read this question. The answer says that even in Java 8 (where we can have default methods in interfaces), we cannot have default constructors. And it says that it makes no sense. Can someone explain why it doesn't make any sense or whatever the…
Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
2
votes
0 answers

In Java should helper methods be static or instance?

I know this question has been answered by others before, but the answer seems to be different depending on the situation. I see many people saying the class should be instantiated, so that it can be tested and mocked etc... Well here's my code and…
Adam.J
  • 2,519
  • 3
  • 14
  • 12
2
votes
0 answers

Why wasn't Java 8's Stream API implemented using default methods

I recently started using Java 8's Stream API and I think it's a great contribution to Java. However, I don't understand why it was implemented the way it was. One of the other, most wonderful features in Java 8 is default-methods in interfaces,…
Dean Gurvitz
  • 854
  • 1
  • 10
  • 24
2
votes
1 answer

Gephi's java default method not implemented in C# with an ikvm-from dll library

I have very few knowledges in java so I maybe misunderstood my problem: I'm working on the Gephi API which is in Java, and I used IKVMC to work on a dll. I tried to create an empty graph as explained here in the Gephi doc…
elpha01
  • 196
  • 2
  • 4
  • 19
2
votes
3 answers

Java default Interface Methods

Here is my simple code from a Java Tutorial. public interface AnimalIntf { default public String identifyMyself(){ return "I am an animal."; } } I get an error: illegal start of type interface methods cannot have body. The method…
Yuliana
  • 31
  • 1
  • 3