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

Call default method from other interface in another default method

This code crashes: public class MyClass { public static void main(String args[]) { Person person = new Person(); person.selectMe(); } } class Entity {} interface EntityNameable { default String…
J. Doe
  • 12,159
  • 9
  • 60
  • 114
0
votes
0 answers

How to throw compilation error if interface default methods are used?

I do not want the interfaces to have default methods in the code base owned by my team. Is it possible to compile with some specific flag which will error out for the same? I do not care about any dependencies having interface default methods.
0
votes
4 answers

default method in interfaces

For a while I have been puzzled as to why we need default methods in Interfaces, and today I was reading about it. Even though few of my questions are answered, I still have a few queries. Let's take a simple example, I've an interface1. Class A and…
Logan
  • 2,445
  • 4
  • 36
  • 56
0
votes
1 answer

Default method with UnsupportedOperationException implementation

I need to fix a bug in my project, but it turns out the root cause is an effect of many workarounds spread for all implementations of an interface due to a design problem. I want to refactor that interface, but I can't do it now, cause I don't have…
Diego Marin Santos
  • 1,923
  • 2
  • 15
  • 29
0
votes
2 answers

Overload Default Interface Method in Java 8

Why is it not possible to overload default methods in Interface in Java 8.
0
votes
3 answers

How to initializing Class at interface default method?

I have an interface: public interface ITransformer{ public void transform(S source,T target); default String getTransformerName(){ Class s; Class t; return s.getName() + t.getName(); …
Seng Zhe
  • 613
  • 1
  • 11
  • 21
0
votes
0 answers

Java 8 abstract class with Interface having default method

I have an interface A with a default method printme(), same method I am having in an abstract class B. If I am extending abstract class B with a TestClass and implementing interface A, printme() method is not visible in TestClass. Below is…
Vivek Panday
  • 1,426
  • 2
  • 16
  • 34
0
votes
1 answer

Class 3 implements i and extends c, i has a default method and c has a same method with different implementation. Which implementation applies for 3?

If I have a interface i and a class c, i has a default method (Java8) and c has a method with the same signature but with a different implementation. Which implementation applies to a class 3 that implements i and extends c?
0
votes
1 answer

Are there any good examples of default methods and their usage in the Java 8 JDK?

As the official statement about virtual extension methods (default methods) states: The purpose of virtual extension methods (default methods) is to enable interfaces to be evolved in a compatible manner after their initial publication.” Can I…
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
0
votes
1 answer

Is calling a superinterface's default method possible?

Say I have two classes, A and B: class A { void method() { System.out.println("a.method"); } } class B extends A { @Override void method() { System.out.println("b.method"); } } After instantiating B as…
The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
-1
votes
1 answer

Using default class members in VBA projects used as external reference

I tried to add one VBA project Lib as reference for a second VBA project App and the default class members don't work. Is this a limitation of VBA? Or is there something I need to do to get it to work? Here are some more details: In Lib I have…
stenci
  • 8,290
  • 14
  • 64
  • 104
-1
votes
1 answer

This keyword in a default method

I know that my question is dumb, but things are a little unclear in my head. I created an interface who has a default method, let's name it Dumb(). What I am trying to do is to use this.argument somewhere in this method, but that didn't work. I know…
Cipri
  • 69
  • 1
  • 8
-1
votes
3 answers

Core Java interface reusablity

A java interface say "TestInterface" having 2 methods method1(), method2() is implemented by 100 different classes. Now I need to introduce a new method in TestInterface without making changes to other classes which already implemented it. How do I…
rakeshG
  • 11
  • 2
-1
votes
3 answers

Full Fledged Multiple Inheritance in Java 8

It seems that Java 8 allows full fledged inheritance with a simple framework as below, using Static and Default methods on interfaces. While its always possible to misuse and write stupid code, these new features make it quite easy to achieve…
Gonen I
  • 5,576
  • 1
  • 29
  • 60
-2
votes
2 answers

static public methods and non static default methods in interfaces

While browsing the source of java.util.Collections interface of java 1.8, I noticed this, default boolean removeIf(Predicate filter) { .... } So as of 1.8, java allows methods in interfaces to define bodies. However there a few…
Rnet
  • 4,796
  • 9
  • 47
  • 83
1 2 3
11
12