Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use double interfaces in multiple implementing classes in Java?

Tags:

java

oop

So lets assume I am having the following stuff defined:

public interface IExportTool {
    void export(IReport iReport);
}

And then attempting to use it:

public class KibanaExporter implements IExportTool{

    public void export(IReport kibana) {
        kibana = (Kibana) kibana;
        ((Kibana) kibana).toJSON();
    }
}

But there are also other classes which would again be doing something like that too:

public class MetricExporter implements IExportTool{

public void export(IReport metric) {
    metric = (Metric) metric;
    ((Metric) metric).toJSON(); // might be something else here like toXML etc
}

}

Please note that both Kibana and Metric are implementing IReport<KibanaRow> and IReport<MetricRow> respectively, while the IReport interface looks like:

public interface IReport<T> {
    void addRow(T row);
}

I don't like all this casting, this doesn't feel right nor gives me autocomplete, so any suggestion how to do it properly?

like image 503
Carmageddon Avatar asked Sep 02 '26 08:09

Carmageddon


1 Answers

From what you've posted, it's clear that both Kibana and Metric are subtypes of IReport.

In that case, you can make the interface generic:

interface IExportTool<R extends IReport> {
    void export(R iReport);
}

And then change the implementations in this fashion:

public class KibanaExporter implements IExportTool<Kibana>{

    public void export(Kibana kibana) {
        kibana.toJSON();
    }
}

And:

public class MetricExporter implements IExportTool<Metric> {
    public void export(Metric metric) {
        metric.toJSON();
    }
}

This version allows the compiler to understand and validate that only instances of subtypes of IReport will ever be passed to export(). Code using this will be validated by the compiler, such that MetricExporter().export() can only be called with an object of type Metric and KibanaExporter().export() with an object of type Kibana.
And with that, type casts are no longer needed.

like image 143
ernest_k Avatar answered Sep 04 '26 22:09

ernest_k



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!