Over a million developers have joined DZone.

Pushing Runtime Behavior Without Lambdas

DZone 's Guide to

Pushing Runtime Behavior Without Lambdas

Lambdas are a lot of fun, but they might not always be necessary. If you're not using Java 8, you can use Interface and Anonymous classes to do largely the same thing.

· Java Zone ·
Free Resource

In Java 8, one of the best features is lambda expressions. The entire idea is passing a behavior to a method. Inspired by functional programming, we can pass methods as arguments of other methods. Methods taking other methods as arguments are called higher order functions.

The benefit of this type of design is you can pass behavior and values together so that a single method can perform all types of actions on those values.

Say I want to create a calculator. In Java, we have to create methods like add, subtract, multiply etc. But in the functional programming paradigm, we create one method that takes another method as an argument, where we define what to do.

Here's the pseudo code of a higher order function:

function void caculate (a,b,m){


calculate(10,20,funtion(y,z){// define and pass the method on fly

In Java 8, using lambdas, we can do the same thing.

But here, we will discuss how we can achieve the same thing without lambdas, as may, many projects haven't adopted Java 8 due to some restrictions.

We can achieve the same thing by using Interface and Anonymous class.

Creating an Interface

Step 1: Create an Interface which will provide the contract for the behavior/strategy, such as add, subtract, multiply, etc.

package com.example.runtime.behaviour;

public interface IStrategy {
   public void operation(int operand1,int operand2);

Creating a Class

Step 2: Next, I will create a Calculator class where we create a method called calculate, which takes two operands and the IStrategy interface as arguments.

It acts like functional programming, particularly higher order functions, where we can pass method/behavior runtime.

While calling this calculate method, I pass the actual implementation of IStrategy interface by using an Anonymous class.

This way, we can push behavior on runtime without lambda expressions.

package com.example.runtime.behaviour;

public class Calculator {

   public void caculate(int operand1,int operand2,IStrategy runtimeBehaviour){
      runtimeBehaviour.operation(operand1, operand2);

   public static void testAllOperation(){

      Calculator calc = new Calculator();
      calc.caculate(10, 20, new IStrategy(){
          public void operation(int operand1, int operand2) {
              int result = operand1+operand2;
              System.out.println("ADD:: " + result);            
      calc.caculate(10, 20, new IStrategy(){
          public void operation(int operand1, int operand2) {
              int result = operand1*operand2;
              System.out.println("Multiply:: " + result);            

   public static void main(String[] args) {

Look at the testAllOperationMethod here. We pass two operands, 10 and 20, and the implementation of IStrategy is for adding another for multiplication.

So rather than creating four different methods (add, multiply, subtract, and division), we can create one method that will support all possible operations taken on operand1 and operand2.

In the future, if any new operation, like modulus, will be added, it can be easily incorporated into this approach. But if we use one method for one operation, then you need to add a new method called modulus to the Calculator class, which will break the open-close principle.

java ,lambda expressions ,interface ,anonymous class ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}