2

Can I override toString method of functional interface? Or rephrase. Is there elegant way to change anonymous inner class that implements functional interface and overrides toString method with lambdas? Can I override toString when I create lamba expression in JDK8.

interface Iface {
    void do();
}

main() { 
    Iface iface = () -> /*do something*/
    System.out.println(iface); // I would like to see anything useful in output
}

Can I override toString for iface?

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
Andrei N
  • 4,716
  • 4
  • 29
  • 29
  • @Keppil I asked about how to do that – Andrei N May 14 '13 at 13:38
  • 3
    @skydreamerr, StackOverflow is not a bespoke service where you tell the community what you want and we develop code for you. Demonstrate that you have at least made an effort by showing the code you've written so far and we'll help you sort out what's wrong. – Derek Henderson May 14 '13 at 14:10
  • 1
    @Derek I don't ask you to develop some algorithms for me. I asked general question is there way to override toString method in functional interface. – Andrei N May 14 '13 at 15:38
  • 1
    Or rephrase. Is there elegant way to change anonymous inner class that implements functional interface and overrides toString method with lambdas? – Andrei N May 14 '13 at 15:44
  • If it is for debugging purpose only and you do not care about performance while debugging take a look at http://stackoverflow.com/a/42876841/1325574 – Sebastian Mar 19 '17 at 07:00

3 Answers3

8

If you own the interface, you can do something like this

public interface Iface {
    void doIt();

    default Iface withToString(final String toString) {
        return new Iface(){
            public void doIt(){
                Iface.this.doIt();
            }

            public String toString(){
                return toString;
            }
        };
    }
}

public static void main(String... args){
    Iface iface = () -> {};
    iface = iface.withToString("anything useful");
    System.out.println(iface); // prints "anything useful" to output
}

Of course in practice it's fun to use something more interesting than just a String. It's not too much more code to capture the arguments and return value from doIt (if there were any) and generate a custom string with another functional interface.

Fuwjax
  • 2,327
  • 20
  • 18
  • That's a great answer. Thanks. BTW `final` is redundant. – gamliela Dec 15 '15 at 19:17
  • I'm not sure I'd call it redundant, but it's certainly not important here. FindBugs got me hooked on putting final everywhere, but it's obviously not a consistent practice for me. – Fuwjax Jan 05 '16 at 02:38
6

No, lambda expressions are used to express one method interfaces as if they are just functions. It's an element of functional languages that was implemented in Java (an OOP language).

To override toString, you must implement the interface in a class.

fhelwanger
  • 465
  • 4
  • 9
1

Every Lambda Expression implements one @FunctionalInterface. So you have always the choice to pass an interface with the same type instead of a lambda itself. You can even create a new implementation (anonymous inner class, inner class, or standalone class) of this interface. But if you care about the syntax of lambdas or what to pass a method reference you can also try one of the following answers to Creating String representation of lambda expression or Naming(toString) Lambda-Expressions for Debugging purpose

Community
  • 1
  • 1
Sebastian
  • 136
  • 1
  • 7