Scala class to implement two Java Interfaces - how? - java

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 {
//...
}

Related

Java GenericClass< ? extends Interface> as a Parameter

public interface A {}
public interface B {}
public class Test implements A{}
public class Test2 {}
I made a method which checks if a class implements an interface. I want my method to only accept generic interfaces and not all class objects.
method
public static boolean containsInterface(Class clazz, Class intf)
{
try
{
Validate.isTrue(intf.isInterface());
if(clazz.isInterface())
return JavaUtil.isClassExtending(intf, clazz);
for(Class c : ClassUtils.getAllInterfaces(clazz))
{
if(JavaUtil.isClassExtending(intf, c))
return true;
}
}
catch(Throwable t)
{
t.printStackTrace();
}
return false;
}
Since Test.class & Test2.class are not interfaces on the 2d parameter I want it to have a compile error since the 2d parameter must be an interface class
containsInterface(Test.class, Test.class);
containsInterface(Test.class, Test2.class);
acceptable use of my method
containsInterface(Test2.class, A.class);
containsInterface(Test.class, B.class);
what I tried
public static boolean containsInterface(Class clazz, Class<? extends Interface> intf)
I currently check if the class in the parameter is an interface then throw an exception. I would rather force people to not be able to call the method to begin with if it's not an interface
I am aware of Annotations and Enum objects are available to use as a class signature to make sure people are using the parameters right but, I can't seem to find the one for the interface itself.
I was told generics do not support interfaces or abstract classes type is this true even in jre 9-13+
You cannot force argument to be ANY interface implementation using type control system. The same would apply if you would like to eg force only Class<?> with abstract modifier. I am not sure if that is really needed as doing simple type isInterface assert is
Straigthforward
Clean
Robust
Easy to understand
Error prone
Testable
JDK Engineers does not care about that either. As an perfect example of such mechanism would be used (but there is none) is JDK Dynamic Proxy creation. You can create only create proxy of an interface (or set of) but not of class.
I don't think that it is just worth of the effort to write own preporocessors. Moreover it would be not universal - as you assume that runtime type must be know at compile time - what about dynamically loaded classes etc?
Intefaces in java has no super class that you can use in generic mode.
If you try get the super class of an interface with reflection returns null.
public static void main (String [] args) {
System.out.println(A.class.getSuperclass());
}
interface A {}
Output:
null

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

Implement Java Interface in a Groovy class

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

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!

Categories

Resources