How to init a inferface in jenkinsfile

This title might be a little bit confusing cause my English is not that good and I hope you can understand what I am saying. What I am trying to say is I have an interface and a class Jenkins shared lib(see below).

What I want is to new an instance of that interface but the class

So what I can do now is:
import org.baseFunctions.widelyUsedFunction
def commonfunctions = new widelyUsedFunctions(env,steps)
commonfunctions.printTest(“interface test”)

And I know this could work but I did not new an instance of the interface, What I want is something like this:
import org.baseFunctions.widelyUsedFunctions
import org.baseFunctions.widelyUsed
widelyUsed widelyUsed = new widelyUsedFunctions(env,steps)
widelyUsedFunctions.printTest(“interface test”)

However it not working.
Could anyone help me?

First it is common in Java that the names of your interface/class should start with a capital letter

interface WidelyUsed
class WidelyUsedFunction

by definition in Java your commonfunctions variable is an instance of widelyUsed

And in your code you refer to a class and not an object
It should look like this:

import org.baseFunctions.WidelyUsedFunctions
import org.baseFunctions.WidelyUsed
WidelyUsed widelyUsed = new WidelyUsedFunctions(env,steps)
widelyUsed.printTest(“interface test”)
1 Like

Omg. It actually works! Thank you so much!