4

I have an interface called Relation, implemented by a class BasicRelation, and extended by subclasses (e.g. ParentChild, Sibling, Spouse). While developing my code, I realized that I often need a method which takes a String representation of a relation to create it. For example:

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

Now, since I find myself needing this method (fromSentence(String)) in every class, except perhaps in BasicRelation, I would like to move it up the hierarchy. The problem is that the internal details of the method is subclass-dependent, so I can't have it as a static method in the interface Relation or the superclass BasicRelation.

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

Is there any way to ensure that every subclass of BasicRelation (or every class implementing Relation) implements fromSentence(String)? If no, should I be designing this in a completely different way? I guess this last question is more of a request for design-advice than a question.

8
  • 1
    No, there is no way to force the implementation of a static method. Commented Oct 2, 2014 at 3:36
  • 1
    Why does it need to be static? Commented Oct 2, 2014 at 3:36
  • 3
    Some kind of factory would better apply here. Commented Oct 2, 2014 at 3:37
  • or you could just use some other class with a static method such as fromSentence(String,Relation)? Commented Oct 2, 2014 at 3:51
  • 1
    Those subclass-specific things should be done by the subclass through polymorphism. Or check out the Visitor pattern. Commented Oct 2, 2014 at 4:22

4 Answers 4

1

Why does the static method need to be in the interface? What's stopping you from having a 'Utility' class and having the method in there?

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

As a static method, there is no reason other than access to private members, which can also be accomplished by by 'default' permissions on those members....

Sign up to request clarification or add additional context in comments.

Comments

0

You can try making the BasicRelation class an abstract class and use an abstract fromSentence(..) method. This would require the ParentChild class to override and implement the fromSentence method because you can't 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's implementation } 

}

1 Comment

But static methods can't be abstract. That is essentially how my entire dilemma began. I would like to keep my method static becuase it's independent of a class instance; in fact, it is used to generate a list of instances.
0

If I understood right... you can try an approach like this

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

And then you could have:

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

You will probably need to change the code a bit and add some Generics around to make it happen the way you want. Sotirios Delimanolis Factory suggestion might also be an option.

2 Comments

Both approaches suggested by you are fine for non-static methods. But I would like to keep the method static because it's being called to generate a list of subclass instances, and the method itself is independent of an instance.
Well, in this case, again, a Factory is indeed what you need.
0

You can have the abstract class BasicRelation include the static method which throws an Exception. That way you will be forced to override (shadow) the static method in the subclasses when you use it.

Something like:

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

1 Comment

Hmm ... not sure if it's a good idea "design-wise", but definitely an interesting approach!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.