About this question
I'm defining a class structure for persisting to our cassandra database, and I'm unsure about using a combination of an abstract class and an interface. I have two concrete classes, one for persisting reports and the other for persisting configurations. They both have separate entity classes defined elsewhere that the entityMapper uses. I have read this post which is very similar, but I don't think their example really highlights what I want to know.
My structure looks like this:
Interface
public interface CassandraRepository{
void save(T object);
void delete(T object);
}
Abstract class
import com.datastax.driver.core.Session;
import com.datastax.driver.mapping.Mapper;
import com.datastax.driver.mapping.MappingManager;
abstract class AbstractGenericRepositoryimplements CassandraRepository {
private final Session sesion;
MapperentityMapper;
AbstractGenericRepository(final Session session) {
this.session = session;
final MappingManager manager = new MappingManager(this.session);
this.entityMapper = manager.mapper(getRepositoryClass());
}
protected abstract ClassgetRepositoryClass(); }
Configuration class
import com.model.Configuration;
import com.datastax.driver.core.Session;
public class ConfigurationRepository extends AbstractGenericRepository{
public ConfigurationRepository(final Session session) {
super(session);
}
@Override
public void delete(final Configuration object) {
this.entityMapper.delete(object);
}
@Override
public void save(final Configuration object) {
this.entityMapper.save(object);
}
@Override
protected ClassgetRepositoryClass() { return Configuration.class;
}
}
Report class
import com.model.Report;
import com.datastax.driver.core.Session;
public class ReportRepository extends AbstractGenericRepository{
ReportRepository(Session session) {
super(session);
}
@Override
public void save(Report object) {
this.entityMapper.save(object);
}
@Override
public void delete(Report object) {
this.entityMapper.delete(object);
}
@Override
protected ClassgetRepositoryClass() { return Report.class;
}
}
My question is: is there a point of having the interface at all? Could I simply define both the save and delete methods as abstract methods in the abstract class itself, and eliminate the interface.
I've also realised while typing this up that I could just move both the save and delete implementations into the abstract itself since they do the same thing. But in a case where they each implemented the methods differnetly, is there any benefit to having the interface at all?
Why could I not always use an abstract class over an interface, since it allows you to provide abstract methods that must be implemented in subclasses (like an interface), but then also allows you to define common code implementations between them (which interfaces do not allow)?