Groovy web console

subscribe to the feed Subscribe
to this
site

Interface instanceof

Published 4 months ago by Anonymous
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
interface ITest {
    String name
}

public class Test implements ITest {
    int age
}

public class Pillow implements ITest {
    boolean inUse
}

public void printTest(ITest test) {
    if (test instanceof Test) {
        println "Test"
    } else if (test instanceof Pillow) {
        println "Pillow"
    } else {
        println test.getClass()
    }
}

Pillow p = new Pillow()
Test t = new Test()
ITest iT = new Test()
ITest iP = new Pillow()

println "Pillow:"
printTest(p)
println "\nTest:"
printTest(t)
println "\niTest:"
printTest(iT)
println "\niPillow:"
printTest(iP)