Java Interface and Lambda Expressions

Sayan Tapadar
Nerd For Tech
Published in
3 min readJun 25, 2021

I’ve been a Java developer for most of my professional life, and nothing amazes me more than interfaces! Simple yet so powerful. In this article, I’ll write about a few of the ways interfaces can be used.

Feel free to reach out to me on LinkedIn, to have a talk! I love hearing out new ideas, and of course, criticism!

Interface

Let me jump straight to code. A simple interface, with just 1 function. Here are some ways to implement it.

Creating a separate class to implement the interface

public class A implements SimpleInterface {

@Override
public void simpleFunction() {
//some code...
}
}

Instantiating an anonymous class that implements the interface

public class A {    SimpleInterface object = new SimpleInterface() {

@Override
public void simpleFunction() {
//some code...
}
}
// object can now be used anywhere in code like any variable}

Using a lambda expression

public class A {    SimpleInterface object = () -> {
//some code...
}
}

In the above scenarios, we’ve used an interface with a single abstract method. It is generally referred to as “Functional Interface”.

An interface with a single abstract method is a Functional Interface

Why do we need it?

To simplify code! To break code into distinct independent components. To reduce clutter! Let me share a scenario.

Say you have a thread, which might run for an unpredictable amount of time. When it finishes executing, you need to return some data. But the main execution shouldn’t need to wait for the thread to finish. An interface can be great for this scenario!

Here, SimpleInterface is a functional interface. When main() executes, a new object is instantiated and passed to SomeThread. When the thread finishes, it calls simpleFunction(int), thereby passing the data to the implemented function!

Note that SimpleThread doesn't need to concern itself with how Main behaves. And Main doesn't need to concern itself with how SimpleThread behaves.

We “magically” get the data back in our main() method, which can be used from line 26 onwards!

This frequently comes in handy during networking, system calls, waiting for user interaction, and pretty much anywhere where we do not want the normal execution flow to “wait”.

Android

If you’re an Android developer, you must have used View.setOnClickListener(View.OnClickListener)!

With the power of Lambda expressions, it can be simply written as

view.setOnClickListener( v -> {
// code when this view is clicked
});

All examples above showed an interface with a single abstract method. But what if you need more abstract methods? — You wouldn’t be able to use a lambda expression anymore. You could either create a separate class to implement the interface; or instantiate an anonymous class that implements the interface — both of which I’ve mentioned above. You can implement any number of abstract methods here!

I’ll keep the article short and to the point. But before I end it, let me just mention the 3-tier architecture. Interfaces play a huge role in organizing the code this way!

Please do not hesitate to get in touch with me, for a conversation! As an entrepreneur, I can’t brag enough about the help I’ve gotten through conversation!

--

--