1

I'm confused about how should I declare an object that implements more than one interface, or derives from a class that implements one interface, and implements another interface itself.

Let's suppose I have a generic DAO interface, as follows:

public interface IDao<T> {

  Optional<T> get(long id);

  List<T> getAll();

  void save(T t);

  void update(T t, String[] params);

  void delete(T t);
}

Then, I also have an implementation for this interface:

public class DaoImpl implements IDao<Entity> {
  //implementation goes here
}

In my understanding, if I'd like to use this DAO implementation in another class, I should declare the object as an IDao, instead of DaoImpl, in order to be able to change the implementation without modifying the class. See below:

public class MyClass {

  IDao dao;

  public MyClass(IDao dao) {
    this.dao = dao;
  }
}

However, suppose I want to create an implementation that extends the DaoImpl and adds functionality, for example:

public class FilterDaoImpl extends DaoImpl implements IFilterDao<Entity> {
  public List<Entity> getBetweenDates(Date start, Date end) {
  //...
  }
}

I believe I should also create an IFilterDao interface and make the FilterDaoImpl implement it. I'm not sure how to declare this implementation in a class. If I do it like this:

public class MyClass2 {

  IFilterDao dao;

  public MyClass(IFilterDao dao) {
    this.dao = dao;
  }
}

I won't be able to call methods like getAll().

How should I declare the FilterDaoImplimplementation in a class?

2

If the idea is that a FilterDao includes all of the behaviours that a regular Dao does, then you can make FilterDao extend the Dao interface. e.g.

public interface FilterDao extends Dao {

you can then extend the implementation to avoid reimplementing methods:

public class FilterDaoImpl extends DaoImpl implements FilterDao {

By the way, most Java conventions prefer not to prefix interface names with "I".

  • Thanks for the answer. This option came to my mind, but then, the FilterDaoImpl would have to implement all the methods of the Dao and the FilterDao. I wanted FilterDaoImpl to extend DaoImpl just so it could use the already implemented methods of DaoImpl. – Pedro Rates Aug 8 at 12:15
  • @PedroRates then make FilterDao extend Dao, and FilterDaoImpl extend DaoImpl. – Kayaman Aug 14 at 7:17
  • @Kayaman thanks for the insight. At least in Java, it would generate a compiler error saying something along the lines of "FilterDaoImpl is trying to implement Dao twice". – Pedro Rates Aug 14 at 12:51
  • 1
    @PedroRates I'll bet you my 20+ years of experience with Java that it doesn't say that. It's redundant to redeclare an interface implemented in the superclass, but it's not an error. – Kayaman Aug 14 at 12:52
  • @Kayaman you're right. I've just tested it and it worked just right. I tried something similar just before I posted this question and got the aforementioned error. Guess I've messed something up. Don't you want to post an answer? I'll be glad to accept it. – Pedro Rates Aug 14 at 13:08
1

I believe what you want to have a Normal Dao which will read data from DB and then have a FilteredDao which will get Data and the apply filter and return it.

Composition over Inheritance

You can do this way.

  • Have class IDAO

    public interface IDao {
    
        public String getData();
    }
    
  • Dao Impl

    public class DaoImpl implements IDao {
    
           public String getData() {
           return "Hello world";
          }
      }
    
  • IFilterDao

    public interface IFilterDao {
    
        public String getFilteredData();
    }
    
  • IFilterDaoImpl

    public class IFilterDaoImpl implements IFilterDao {
    
        private IDao iDao;
    
        public String getFilteredData() {
            String data =  iDao.getData();
            //apply some filter
           return data;
        }
    }
    
    

Now the IFilterDaoImpl will have all the functionality of IDAO. No matter what impl of DAO being used.

  • Thanks for the answer! I think that in some cases one would prefer getting the data already filtered than filtering it in memory. I was thinking about the latter when I asked this question. – Pedro Rates Aug 16 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.