Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In GWT, why shouldn't a method return an interface?

Tags:

gwt

In this video from Google IO 2009, the presenter very quickly says that signatures of methods should return concrete types instead of interfaces.

From what I heard in the video, this has something to do with the GWT Java-to-Javascript compiler.

  • What's the reason behind this choice ?
  • What does the interface in the method signature do to the compiler ?
  • What methods can return interfaces instead of concrete types, and which are better off returning concrete instances ?

Snippet of code from Google IO presentation showing it's better that your method returns ArrayList than List

like image 487
Leonel Avatar asked May 19 '11 13:05

Leonel


People also ask

Can a function return an interface?

Currently for a function to satisfy an interface, the signature of the function has to match the interface signature exactly. In the case where the interface signature says the function should return an interface, the function can only return that interface, and not another interface which is a superset.

Can an interface be used as a return type for a method?

Methods do not return interfaces or classes. They return a reference to an instance (=object) or null (or a primitive value, but let's stick with objects). This reference is usually either stored in a variable or used to call instance methods or access instance members.

CAN interface have return type in Java?

Note: You also can use interface names as return types. In this case, the object returned must implement the specified interface.


1 Answers

This has to do with the gwt-compiler, as you say correctly. EDIT: However, as Daniel noted in a comment below, this does not apply to the gwt-compiler in general but only when using GWT-RPC.

If you declare List instead of ArrayList as the return type, the gwt-compiler will include the complete List-hierarchy (i.e. all types implementing List) in your compiled code. If you use ArrayList, the compiler will only need to include the ArrayList hierarchy (i.e. all types implementing ArrayList -- which usually is just ArrayList itself). Using an interface instead of a concrete class you will pay a penalty in terms of compile time and in the size of your generated code (and thus the amount of code each user has to download when running your app).

You were also asking for the reason: If you use the interface (instead of a concrete class) the compiler does not know at compile time which implementations of these interfaces are going to be used. Thus, it includes all possible implementations.

Regarding your last question: all methods CAN be declared to return interface (that is what you ment, right?). However, the above penalty applies.

And by the way: As I understand it, this problem is not restricted to methods. It applies to all type declarations: variables, parameters. Whenever you use an interface to declare something, the compiler will include the complete hierarchy of sub-interfaces and implementing classes. (So obviously if you declare your own interface with only one or two implementing classes then you are not incurring a big penalty. That is how I use interfaces in GWT.)

In short: use concrete classes whenever possible.

(Small suggestion: it would help if you gave the time stamp when you refer to a video.)

like image 88
Stefan Avatar answered Oct 10 '22 19:10

Stefan