Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

Should an abstract class implement interface java as opposed to defining its abstract methods?

Asked by Matthew Taylor Oct 11, 2022 1.4K views 1 answer
Share

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 AbstractGenericRepository implements CassandraRepository {
    private final Session sesion;
    Mapper entityMapper;
    AbstractGenericRepository(final Session session) {
        this.session = session;
        final MappingManager manager = new MappingManager(this.session);
        this.entityMapper = manager.mapper(getRepositoryClass());
    }
    protected abstract Class getRepositoryClass();
}
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 Class getRepositoryClass() {
        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 Class getRepositoryClass() {
        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)?

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.