Search code examples
javaspring-bootparametershierarchy

Can somebody explain how parameters work in java hierarchy?


I am new to java inheritance and have difficulties to understand the details of the concept. In my Springboot project I originally had 2 domain classes:

public class Excerpt {
    private int excerptID;
    private String author;
    private String title;
    private String text;
    private String comments;
}

public class Tag {
    private int excerptID;
    private String description;
}

the DAO package consisted of a generic DAO interface

public interface DAO<T> {

    public void save(int id, String... params);
    public List<T> getAll();
    public List<T> getByTitle(String... params);
    public List<T> getByTag(String... params);
    public List<T> getByID(int excerptID);
    public int countAll();
    public void delete(int id);
}

and 2 class implementations

public class ExcerptDAO implements DAO<Excerpt> { ... }

public class TagDAO implements DAO<Tag> { ... }

So far so good. But then I decided to add another domain class Outline which would be sharing Tag database table with Excerpt

public class Outline {
    private int outlineID;
    private String title;
    private String plot;
    private String comments;
    private String genre;
    private String resources;
}

Also the DAO methods would be partly overlapping, so I added another level of interfaces. The umbrella DAO interface:

public interface DAO<T> {

    public void save(int id, String... params);
    public List<T> getAll();
    public List<T> getByTitle(String... params);
    public List<T> getByTag(String... params);
    public List<T> getByID(int id);
    public int countAll();
    public void delete(int id);
}

ExcertpsDAO and OutlinesDAO extending interfaces

public interface ExcerptsDAO extends DAO<Excerpt>{
    public List<Excerpt> getByAuthor(String... params);
}

public interface OutlinesDAO extends DAO<Outline> {
    public List<Outline> getByGenre(String... params);
    public List<Outline> getByResource(String... params);
}

and I am about to implement them into classes but do not understand an error I am getting:

public class ExcerptDAO implements ExcerptsDAO<Excerpt> { ... }

The type ExcerptsDAO is not generic; it cannot be parameterized with arguments <Excerpt>

evidently this is correct

public class ExcerptDAO implements ExcerptsDAO { ... }

but I do not understand why the parameter is not accepted when it is the same as in the ExcerptsDAO interface. Why should it matter that the interface is not generic if the parameter is the same?


Solution

  • If a class or interface doesn't take generic parameters you're not allowed to add some, much like you're not allowed to pass parameters to a method that takes none. So ExcerptsDAO has_no_ generic parameters and already fixes the generic type passed to DAO to be Excerpt. So you can't change the parameter and thus it's not allowed to add one.

    To summarize: public class ExcerptDAO implements ExcerptsDAO { ... } is perfectly fine since ExcerptsDAO will always only deal with Excerpt instances.

    If you'd do ExcerptsDAO<T extends Excerpt> implements DAO<T> it would work but then you'd have to always define a generic type parameter for ExcerptsDAO since it could as well be something other than Excerpt.