Implement Java Interface in a Groovy class - java

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.

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

Related

Interface without polymorphism

I have recently reviewed a few spring projects.
I saw some interface which created for one class in dao and service layer of some projects.
like this:
public interface EmployeeDao(){
//some methods declaration
}
public class EmployeeDaoImp implements EmployeeDao (){
// methods overriding
}
public interface CompanyDao(){
//some methods declaration
}
public class CompanyDaoImp implements CompanyDao (){
// methods overriding
}
there is no need to Polymorphism in this case.
why we need to these interfaces?,what is advantage of these?.
I hope I could express myself.
Spring uses interfaces a lot because they follow the design principle "Program to an interface not to an implementation"
See Programming to an interface

How to get annotations of interface or abstract class methods in Java

I have an interface like this:
public interface IFoo{
#AnnotationTest(param="test")
String invoke();
}
and I implement this like this:
public class Foo implements IFoo{
#Override
public String invoke(){
Method method = new Object() {
}.getClass().getEnclosingMethod();
AnnotationTest ann = method.getAnnotation(AnnotationTest.class);
if(ann == null){
System.out.printl("Parent method's annotation is unreachable...")
}
}
}
If it is possible to reach parent's annotation, I want to learn the way of it.
Any help or idea will be appreciated.
You can use Spring AnnotationUtils.findAnnotation to read annotations from interfaces.
Example :
Interface I.java
public interface I {
#SomeAnnotation
void theMethod();
}
Implementing class A.java
public class A implements I {
public void theMethod() {
Method method = new Object() {}.getClass().getEnclosingMethod();
SomeAnnotation ann = AnnotationUtils.findAnnotation(method, AnnotationTest.class);
}
}
It obviously requires to include in your project (and import) Spring framework classes.
There is no direct way to get it. If you really need, you have to manually loop over getInterfaces() to find if any implemented interface has the annotation. If you want to search for (eventually abstract) superclasses and the annotation is not #Inherited, you can again iterate the superclass chain until finding Object (*).
But beware, as following post states, there are good reasons for this not to be directly implemented in Java : Why java classes do not inherit annotations from implemented interfaces?
(*) If the annotation is #Inherited it is automatically searched on superclasses, but not on interfaces.
you can't inherit annotations.
But a framework that uses an annotation can check to see if annotation is present on superclass

Generics and API design

I am building a library for a product we will be releasing and am trying to design it for compatibility with some design alterations that are presently on the road map. Specifically the library needs to handle different versions of a product which shares the same commands but which have different requirements for the available options for parameters. To accomplish this I have an abstract class for the product with concrete classes for each of the versions. Additionally I have an abstract class for the peripheral which varies and concrete classes for the specific peripherals. I want the concrete versions to implement an abstract method from the abstract class but for a specified type T rather than specifying the super class and then checking instanceof. For example:
class PeripheralA {}
class PeripheralB {}
abstract class AbstractProduct<T> {
public abstract void SomeFunction(T param);
}
class ProductA extends AbstractProduct<PeripheralA> {
public void SomeFunction(T param);
}
class ProductB extends AbstractProduct<PeripheralB> {
public void SomeFunction(T param);
}
The problem is the compiler says I am not implementing the abstract method SomeFunction. My previous design was to not use generics and specify the abstract type. The problem here is that if PeripheralB is passed to ProductA there will be a class cast exception which I can catch, but I would prefer that the code not compile in the first place (and for that matter, not use the class cast). Is there a way to pull off what I am trying to accomplish or am I simply going about the design incorrectly?
Don't use T for the type of the method parameter; use the type you specified for the class, ie PeripheralA, and you must provide a method body for a non-abstract class:
class ProductA extends AbstractProduct<PeripheralA> {
public void SomeFunction(PeripheralA param) {
// your impl here
}

java interface is analogus to java specification?

Can I take as a example to make me/other understand the diff between a interface and class as
java interface is analogus to java specification(JMS APIs)
Java Class is analogous to implementation(ActiveMQ etc) of the specification
An easy way to understand the difference is to think that an interface defines what an object does, while a class defines how it does it.
The analogy that you are suggesting is incomplete, because Java has a concept that is in the middle of an interface and a class, i.e. an abstract class: a specification may be modeled as an abstract class or as an interface.
The difference between a class and an interface is the way they are ran. A class has a main method inside of it. Interfaces are like global files you can place methods into. For example, you have the class Blah
public class Blah {
// NOTICE: this is a method that allows you
// to perform actions within the method
public static void main(String args[]) {
// do whatever
}
}
and you have an interface named BlahInterface
public interface BlahInterface {
public static void main(String args[]);
public void sayBlah();
public int getAmountOfBlahs();
public String getWhatBlahSays();
public int setBlahs(int blahNumber);
}
As you can see, the difference between the class and interface is that the class relies on the interface for the methods that it can implement.
You can see that the class allows the main method to run things within itself, but if you were to try to run the main method within the interface, it wouldn't work.
Summary:
The interface is only to initialize the methods that the class can use/run.
Hope this helps!

Scala class to implement two Java Interfaces - how?

I have just started learning Scala and I'm now wondering how I could implement two different Java interfaces with one Scala class? Let's say I have the following interfaces written in Java
public interface EventRecorder {
public void abstract record(Event event);
}
public interface TransactionCapable {
public void abstract commit();
}
But a Scala class can extend only one class at a time. How can I have a Scala class that could fulfill both contracts? Do I have to map those interfaces into traits?
Note, my Scala classes would be used from Java as I am trying to inject new functionality written in Scala into an existing Java application. And the existing framework expects that both interface contracts are fulfilled.
The second interface can be implemented with the with keyword
class ImplementingClass extends EventRecorder with TransactionCapable {
def record(event: Event) {}
def commit() {}
}
Further on each subsequent interface is separated with the keyword with.
class Clazz extends InterfaceA
with InterfaceB
with InterfaceC {
//...
}

Categories

Resources