Reputation: 47
My requirement is I have an Interface where one method is declared and two classes implement the same interface, in which one class has return type of Set<object type>
and other LIST<object type>
... please help me in correcting the code mistakes.
Here is the example code below :
Report file --- interface
public interface Reportfile
{
public <T> parseReadfile();
}
Masterfile.java ---- class one
public class Masterfile implements Reportfile
{
Set<Masterpojo > mset = new TreeSet<Masterpojo>();
public String[] newline = null;
public Set<Masterpojo> parseReadfile()
{
try{
Masterpojo mo = new Masterpojo();
CSVReader mread = new CSVReader(new FileReader(mfile),(delimiter));
while ((newline = mread.readNext()) != null)
{
mo.setmline(newline);
mset.add(mo);
}
}catch(Exception e)
{
log.error("file not found");
}
return mset;
}
}
Transfile.java ---- class 2
public class Transfile implements Reportfile
{
List<Transpojo> tlist = new ArrayList<Transpojo>();
public String[] newline = null;
public List<Transpojo> parseReadfile()
{
try{
Transpojo to = new Transpojo();
CSVReader tread = new CSVReader(new FileReader(Transfile),(delimiter));
while ((newline = tread.readNext()) != null)
{
to.settline(newline);
tlist.add(to);
}
}catch(Exception e)
{
log.error("Transaction file not found");
}
return tlist;
}
}
Masterpojo
public class Masterpojo
{
public String[] mline = null;
public String[] getmline()
{
return mline;
}
public void setmline(String[] mline)
{
this.mline = mline;
}
}
Transpojo
public class Transpojo
{
public String[] tline = null;
public String[] gettline()
{
return tline;
}
public void settline(String[] tline)
{
this.tline = tline;
}
}
I error like belo when I compile the code in cmd :
Masterfile.java:6: error: Masterfile is not abstract and does not override abstract method <E>parseReadfi
le() in Reportfile
public class Masterfile implements Reportfile
^
where E is a type-variable:
E extends Object declared in method <E>parseReadfile()
Masterfile.java:34: error: parseReadfile() in Masterfile cannot implement <E>parseReadfile() in Reportfil
e
public Set<Masterpojo> parseReadfile()
^
return type Set<Masterpojo> is not compatible with void
where E is a type-variable:
E extends Object declared in method <E>parseReadfile()
Transfile.java:7: error: Transfile is not abstract and does not override abstract method <E>parseReadfile
() in Reportfile
public class Transfile implements Reportfile
^
where E is a type-variable:
E extends Object declared in method <E>parseReadfile()
Transfile.java:33: error: parseReadfile() in Transfile cannot implement <E>parseReadfile() in Reportfile
public List<Transpojo> parseReadfile()
^
return type List<Transpojo> is not compatible with void
where E is a type-variable:
E extends Object declared in method <E>parseReadfile()
4 errors
Upvotes: 0
Views: 1613
Reputation: 4091
public interface Reportfile<T>
{
public T parseReadFile();
}
public class Masterfile implements Reportfile<Set<Masterpojo>>
{
public Set<Masterpojo> parseReadfile()
{
...
}
}
public class Transfile implements Reportfile<List<Transpojo>>
{
public List<Transpojo> parseReadfile()
{
...
}
}
However, your code contains massive duplication ! Look at Transpojo
and Masterpojo
. The are exactly the same ! (except naming)
Look at Masterfile
and Transfile
, both parseReadFile
are implemented in the exact same way ! Except that one returns a Set
and the other a List
. All this can be simplified into something like (generics are not needed anymore):
public interface Reportfile
{
public Collection<Pojo> parseReadFile();
}
public final class CsvFile implements Reportfile {
@Override
public Collection<Pojo> parseReadFile() {
Collection<Pojo> result = new ArrayList<>();
String[] newline;
try {
CSVReader reader = new CSVReader(new FileReader(file), delimiter);
while ((newline = reader.readNext()) != null) {
result.add(new Pojo(newline));
}
} catch (Exception e) {
log.error("file not found");
}
return result;
}
}
With Pojo
:
public final class Pojo {
private final String[] line;
public Pojo(String[] line) {
this.line = line;
}
public String[] getLine() {
return line;
}
}
Upvotes: 2
Reputation: 314
Your Interface should declare what T is for the method parseReadfile (not the method itself) so if you make it so T is a generic type of ReportFile you can declare it in the classes that implement it.
For ReportFile.java
public interface Reportfile<T> { public < T > parseReadfile(); }
For Masterfile.java
public class Masterfile implements Reportfile<Masterpojo>
For Transfile.java
public class Transfile implements Reportfile<Transpojo>
You also have to change the method to return T not get T now like so:
public T parseReadfile();
Upvotes: 1