0

I'm writing a little functional programming piece of code. I have an interface, and a class which has a function that gets an implementation of that interface as a param.

  public interface Statement
  {
      T executeTStatement();
  }   

  public class StatementExecutor 
  {

    public static<T> T executeStatement(Statement statement)
    {
         T result;
         try
         {            
             .
             .
             result = statement.executeStatement();
             .
             .
         }
         catch.....
         finally....

         return result;

    }
  }

So the StatementExecutor class and executeStatment function in class are fine, since executeStament declared with static and does not require to have in class declaration. But since static is an illegal modifier for the interface method, the same cannot be done in the interface, I only can add to interface declaration, and I want avoid it cause there will be only one generic method in that interface. So the question is, if I get to such situation there something wrong in my design, or it just limitation of java ? And is there some trick to declare generic method in interface without declaring it on interface level ?

Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85
Dabo
  • 2,371
  • 2
  • 18
  • 26
  • I didn't understand the question... but somewhere in there you are right that you can not define `static` methods in your interface. The type itself is instance of `Class>` and that can not have any custom interface attached. – Pavel Horal Jan 08 '14 at 07:56
  • http://stackoverflow.com/questions/512877/why-cant-i-define-a-static-method-in-a-java-interface, http://stackoverflow.com/questions/2806039/alternatives-to-static-methods-on-interfaces-for-enforcing-consistency – Pavel Horal Jan 08 '14 at 07:58
  • @PavelHoral My interface declaration is incorrect, the only way to make it correct is to add to it's declaration : Statement, and my question is maybe there still another way, without adding on interface level to fix my interface, like in class i can add static on method level. Thanks for links. – Dabo Jan 08 '14 at 08:05

2 Answers2

2

You can declare generic methods in your interface like this:

public interface Statement
{
    <T> T executeTStatement();
}   

But in this way it doesn't make to much sense - you have a method that returns a "something", but when will "something" be defined? So T has to be bound in some way to be of any use:

  • declare it at the Interface-Level and have to bind T when defining your reference (but you didn't want to do that)
  • deduce the type T from some argument to the method:

    public interface Statement
    {
        <T> T executeTStatement(T argument);
    }   
    
piet.t
  • 11,718
  • 21
  • 43
  • 52
  • It actually worked for me with this one : T executeTStatement(); – Dabo Jan 08 '14 at 11:33
  • 1
    Your latter example with the wildcard would suggest that the caller can ask for any type which extends `AnotherClass` and receive an object of such a type, but I don't see how that could work. If `T` had to be a supertype of `AnotherClass`, then the method could return an `AnotherClass`, but if `T` can be a subtype I don't see how the method could possibly know which subtype to return. – supercat Jan 08 '14 at 18:25
  • @supercat It looks like it is enough just to return something when you implement the interface. The type of you return is substituted. – Dabo Jan 08 '14 at 19:20
  • A method that returns `T` (no matter how bounded, unless it's bounded by a `final` type, in which case the type variable is pointless) is completely unsafe or useless because the method cannot safely return anything except `null`. – newacct Jan 08 '14 at 19:54
0

David, generally speaking a java interface is a Object Oriented "prototype" for a real object implementation. "Static mathod" are not (properly) Object Oriented, then static method are not allowed on interface.

Let me say "Static method" and "Java interface" are incompatible (please accept my lack of formalism).

Finally the sentence

is there some trick to declare generic method in interface without declaring it on interface level ?

is a little bit controversial, please refine it.

venergiac
  • 7,469
  • 2
  • 48
  • 70
  • I tend to agree with this : "Static method" are not (properly) Object Oriented, but if they allowed it for classes, why it is not allowed interfaces also ? There still a lot of valid OO examples for static methods.A trick meaning is there some other sintaksis for declaring generic method in interface. – Dabo Jan 08 '14 at 08:14
  • I know that it is a java spec....generally speaking interface is for Object implementation. static method is for "Static implementation" – venergiac Jan 08 '14 at 08:17