Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement Java Interface in a Groovy class

Tags:

java

groovy

I have just started the programming in Groovy. I noticed one strange behavior and unable to find any explanation for the same.

I have created a Java Interface TestInterface.java

public interface TestInterface {

    public void m1();

}

I have created a Groovy class TestG.groovy

class TestG implements TestInterface {

}

I have created a Java class TestJ.java

public class TestJ implements TestInterface{

    @Override
    public void m1() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    } 
}

My problem is in TestG why I do not get any error to implement abstract method or declare class as abstract.

What does differ in java and groovy as I needed to implement abstract methods or declare class as abstract in Java but not in Groovy.

like image 543
Anjali Agrawal Avatar asked Sep 16 '15 08:09

Anjali Agrawal


1 Answers

I know this question has been out for a while and answered above but I felt the need to add this.

class TestG implements TestInterface {}

this is still the "java" way of doing things. Using groovy essentially eliminates the need of implementing interfaces (except just as markup interfaces)

In groovy you would just do this:

def myObject = [m1: {-> doSomething()}] as TestInterface
like image 101
Kaus2b Avatar answered Sep 19 '22 02:09

Kaus2b