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
6
votes
2 answers

Confused about "super" keyword in this Java example

In this example on java website's tutorial page. Two interfaces define the same default method startEngine(). A class FlyingCar implements both interfaces and must override startEngine() because of the obvious conflict. public interface OperateCar…
okey_on
  • 2,888
  • 8
  • 28
  • 36
6
votes
1 answer

Spring Integration @ServiceActivator on a Java 8 default interface method

I'd like to use the @ServiceActivator annotation on a Java 8 default interface method. This default method will delegate to another method of this interface depending on business rules. public interface MyServiceInterface { @ServiceActivator …
Stefaan Neyts
  • 2,054
  • 1
  • 16
  • 25
6
votes
2 answers

What happens, if two interfaces contain the same default method?

If I have two interface with the same default method and both are implementing with a class/ See this program. interface alpha { default void reset() { System.out.println("This is alpha version of default"); } } interface beta { default…
Manohar Kumar
  • 605
  • 9
  • 12
6
votes
3 answers

Java Default Method - get type of subclass

I have an interface in which I want to provide a default method to Serialize the inherited classes. I use a JsonSerializer class to do serialization. The method looks like: public interface A { public default String write() { new…
Joshua Kissoon
  • 3,269
  • 6
  • 32
  • 58
6
votes
3 answers

How to synchronize inside an interface default method without using this?

I have a number of default methods in interfaces that need synchronization and it seems that only this is available: default void addUniqueColumns(List names) { synchronized (this) { ... do something } } The problem is, I…
The Coordinator
  • 13,007
  • 11
  • 44
  • 73
5
votes
0 answers

Why is interface default method shadowed by parent private method?

I don't understand why an IllegalAccessError exception is thrown from the example below. The method foo() in Parent cannot be inherited by Child, because it is private. But trying to call the default method foo() leads to the exception. public class…
Andrey
  • 61
  • 4
5
votes
2 answers

What is the point of the Functor -> Applicative -> Monad hierarchy

What is the point of making Functor a super class of Applicative and Monad. Both Applicative and Monad immediately imply the only implementation of Functor that abides by the laws as far as I can tell. But rather I have to type out the same…
HashBr0wn
  • 387
  • 1
  • 11
5
votes
3 answers

Calling Overridden Default Method from Anonymous Inner Class

Consider this code: interface A { default void doA() { System.out.println("a"); } } interface B { void doB(); } class Test implements A { @Override public void doA() { // Works B b = () ->…
Alexander Torstling
  • 18,552
  • 7
  • 62
  • 74
5
votes
1 answer

Java 8 -Two interfaces contain default methods with the same method signature but different return types, how to override?

I understand that if a class implements multiple interfaces containing default methods of same name, then we need to override that method in the child class so as to explicitly define what my method will do.Problem is, see the below code : interface…
Raman Verma
  • 113
  • 9
5
votes
0 answers

Functional Interface with default behavior with Serializable

When an Interface with no default method extends Serializable everythings works fine. But When a default method for that interface comes in picture we have a warning that says: MyInterface.java: serializable class MyInterface has no definition of…
5
votes
2 answers

How to call default method from interface with TestNG tests and Selenium?

I want to know if it is possible to use default methods from interface with TestNG @BeforeMethod annotation? Here is sample, which I tried: @Listeners(TestListener.class) public interface ITestBase { String baseUrl =…
catch23
  • 17,519
  • 42
  • 144
  • 217
5
votes
3 answers

Spring data rest - expose default methods

I have a Person Repository as follows @RepositoryRestResource public interface PersonRepository extends Repository { List findAll(); default List findNewPersons() { return…
5
votes
2 answers

Java 8 default interface methods not recognized as managed bean properties in EL

I am trying to setup my own JSF tag libary. So I created a composite component with an backing interfaces as a blueprint to build a backing bean for this component. public interface CompLogin { String getUsername(); void setUsername(String…
ScreamingTree
  • 302
  • 1
  • 13
5
votes
2 answers

Must implement default interface method?

Here is a simplified example showing my problem: import java.util.List; public interface SingleTask extends List, Runnable { default Runnable get(final int x) { if (x != 0) { throw new IndexOutOfBoundsException(); …
5
votes
1 answer

Java 8 - default methods - concerns for legacy code

Question from a book: In the past (pre-Java 8), you were told that it’s bad form to add methods to an interface because it would break existing code. Now you are told that it’s okay to add new methods, provided you also supply a default…
peter.petrov
  • 38,363
  • 16
  • 94
  • 159