How to create a generic static method in all classes that implement an interface

I have an interface called Relation

, to implement classes BasicRelation

and subclasses of advanced (eg ParentChild

, Sibling

, Spouse

). As I developed my code, I realized that I often need a method that takes a representation of a String

relationship in order to create it. For example:

public class ParentChild implements Relation extends BasicRelation {

  // e.g. "Jack is Emily father. Jill is her mother." will return the list
  // <ParentChild(Jack, Emily), ParentChild(Jill, Emily)>
  static List<ParentChild> fromSentence(String s) {
    ...
  }
}

      

Now, since I need this method ( fromSentence(String)

) in every class except maybe in BasicRelation

, I would like to move it up the hierarchy. The problem is that the internals of a method are subclass dependent, so I cannot use it as a method static

in an interface Relation

or superclass BasicRelation

.

Unfortunately in Java it is also not possible to have a method static abstract

.

Is there a way to ensure that every subclass BasicRelation

(or every class that implements Relation

) implements fromSentence(String)

? If not, should I design this in a completely different way? I guess this last question is more of a design advice request than a question.

+3


source to share


4 answers


Why should a static method be in an interface? What's stopping you from having a Utility class and method there?

public class RelationUtility {
    public static BasicRelation relationFactory(String asString) {
        ....
    }
}

      



As a static method, there is no reason other than accessing private members, which can also be achieved with the default permissions on those members ....

+1


source


You can try to make the BasicRelation class an abstract class and use the abstract fromSentence (..) method. This would require the ParentChild class to override and apply the fromSentence method, because you cannot create an object for ParentChild without implementing fromSentence ()

public abstract class BasicRelation extends Relation(){

   public abstract List<..> fromSentence(String s);
}

public class ParentChild implements Relation extends BasicRelation {

 fromSentence(){
 //parentChild class implementation
}

      



}

0


source


If I understood correctly ... you can try this approach

public class BasicRelation {

    public abstract List<ParentChild> fromSentenceInSubclass(s);

    public List<ParentChild> fromSentence(String s){
        fromSentenceInSubclass(s);
    }
}

      

And then you could:

public class SubclassRelation extends BasicRelation {

    public List<ParentChild> fromSentenceInSubclass(s){
        // do subclass relation stuff
    }

}

      

You will probably need to tweak the code a bit and add some common elements to make it happen the way you want it to. Sotirios Delimanolis Factory's offer could also be an option.

0


source


You can have an abstract BasicRelation class that contains a static method that throws an exception. This way you will be forced to override (shadow) the static method in subclasses when you use it.

Something like:

public abstract class BasicRelation {
    public static List<..> fromSentence(String s) {
        throw new RuntimeException();
    }
}

      

0


source







All Articles