3

I was reading about default methods in Java 8 and I got stuck in one thing - is there a way to invoke default method from interface without implementing it, or using dynamic Proxy? By using just a simple way, like in following methods:

interface DefaultTestInterface{
    default void method1(){
        //default method
    }
}
class ImplementingClass implements DefaultTestInterface{
    public void method1(){
        //default method invocation in implementing method
        DefaultTestInterface.super.method1();
    }
    void method2(){
        //default method invocation in implementing class
        DefaultTestInterface.super.method1();
    }
}
public class Main {
    public static void main(String[] args) {
        //is there any way to simply invoke default method without using proxy and reflection?
    }
}

I read similar questions, but the first was connected only with invocation in implementing method, and two others was connected with dynamic Proxy using reflection and reflection.

Those solutions are quite complicated and I am wondering if there is simpler way of doing it. I also read those articles, but I didn't found solution for my problem. I would be grateful for any help.

Community
  • 1
  • 1
Michał Szewczyk
  • 7,540
  • 8
  • 35
  • 47
  • 4
    So you want to use a instance method without a instance??? – fabian Sep 18 '16 at 09:07
  • 1
    There is some confusion in your question. A *default method* of a java 8 interface is already an implementation. If you don't have a class specific behavior that need a different implementation, you just implement the interface, omitting the method that you want to use as default. If you want to call a method from an *interface* than consider to use the *static* keyword to define it, instead. – Mario Santini Sep 18 '16 at 09:16
  • No @fabian, I was thinking about solution with instantiation included somehow. – Michał Szewczyk Sep 18 '16 at 09:18
  • 1
    A default method is already implemented. You don't have to re-implement it . – wulfgarpro Sep 18 '16 at 09:23

1 Answers1

8

If the interface has only one method, or all its methods have default implementations, all you need to do is creating an anonymous implementation that does not implement the method that you wish to call:

(new DefaultTestInterface() {}).method1();

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you, this is exactly the solution, which I was looking for. – Michał Szewczyk Sep 18 '16 at 09:23
  • @Michał Szewczyk: your question explicitly says “*without implementing it*” and this answer *is* implementing it. That simply doesn’t make any sense. It boils down to you having actually asked “how do I implement an interface?”… – Holger Sep 19 '16 at 14:33
  • 1
    @Holger Technically, you are absolutely right: this *is* an implementation of the interface. However, this degenerate implementation does not look like much of an implementation - in fact, I'm sure that many programmers, myself included, wouldn't spot the implementation on a cursory reading. – Sergey Kalinichenko Sep 19 '16 at 14:47
  • Indeed, but I’m refraining from calling that an advantage. – Holger Sep 19 '16 at 14:52