Pass function as parameter to a function in Apex

OmkarDeokar
Feb 7, 2021

…well sortof …hear me out!

Common Interface

We create interface as above to perform dynamism in calling functions, and that’s all the boilerplate code that we need!!!

Class that Prints ID

Next we implement our interface in our particular class and override our execute function with our required behavior.

In this case we print Id of passed sObject

Class that Prints Name

Here we are printing Name and like wise… you get the point

Class that prints Given string

But what if I want something else altogether like lets say print a given string.

Voila…

Lets use this…

I have my printer function that will conditionally print values one by one.

Implementation

--

--