I have an abstract class with one or two protected methods needed for the subclasses.

abstract class TransformRouteProcessor {

    protected String doX(String arg1){
        //doX code

    protected int doY(String arg1) {
        //doY code

The subclasses will need to implement the processor interface provided by apache camel in order to use them as the parameters for a .process method in a camel route via it's java dsl offering.What I'm struggling to make a decision on is whether the abstract class should implement this interface( which provides a single public method, process(Exchange exchange)), and have the concrete classes implement this, or should each subclass implement the interface separately? I'm struggling with the decision because I have read from some that only the classes that make use of the interface should implement it, but others are saying if the subclasses would implement the interface regardless, and the abstract class should be able to fulfill the contract that it's subclasses would, then the abstract class should implement it, and the subclasses then provide each implementation details of process.

It's worth noting that the subclasses will use the protected methods unaltered, but each implementation of the process method will vary between the subclasses, so the process method in the abstract class need not provide an implemented version of process

  • 2
    Abstract classes can provide a default implementation for your method. You can then override that method in the descendant class if you wish to change the default behavior. If you don't need any default behaviors, an Interface might be more appropriate. – Robert Harvey Oct 15 '17 at 15:47
  • Well I've read that in java, since interface methods are inherently abstract, it doesn't have to, which in my case would make some sense as the process implementations would carry greatly between the subclasses, so process would be just a signature and nothing else in the abstract class. Would that be considered poor design to override the process method with nothing in it in the abstract class vs. not doing anything and having the subclasses override it, or have I picked up your suggestion incorrectly? – jbailie1991 Oct 15 '17 at 15:51
  • 1
    I think whether it's good design or not depends on what your needs are; in particular, whether or not you want to enforce the contract. That's not clear at all from your question. – Robert Harvey Oct 15 '17 at 15:55
  • Well I do want to enforce that the subclasses make use of the protected methods and use the process interface, in this context then based on your comment I think it would make sense; the subclasses are useless with out the interface but also require the protected methods. I think it's better design then to have the abstract class implement the interface – jbailie1991 Oct 15 '17 at 16:32
  • You can't really enforce what an implementation does except to require the interface is implemented (somehow). However, see this answer. By breaking up your larger class into smaller units you can somewhat enlist the type system in ensuring the proper flow through states and methods. – Erik Eidt Oct 16 '17 at 1:40

Your Answer

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

Browse other questions tagged or ask your own question.