20

AFAIK, my class extends parent classes and implements interfaces. But I run across a situation, where I can't use implements SomeInterface. It is the declaration of a generic types. For example:

public interface CallsForGrow {...}

public class GrowingArrayList <T implements CallsForGrow>  // BAD, won't work!
                              extends ArrayList<T> 

Here using implements is syntactically forbidden. I thought first, that using interface inside <> is forbidden at all, but no. It is possible, I only have to use extends instead of implements. As a result, I am "extending" an interface. This another example works:

public interface CallsForGrow {...}

public class GrowingArrayList <T extends CallsForGrow>  // this works!
                              extends ArrayList<T> 

To me it seems as a syntactical inconsistancy. But maybe I don't understand some finesses of Java 6? Are there other places where I should extend interfaces? Should the interface, that I mean to extend, have some special features?

Gangnus
  • 2,817

4 Answers4

25

In the case of generic type variables the compiler doesn't actually care if T is a class, an interface, an enum or an annotation. All it cares about is that it is a type with a given set of sub- and super-types.

And there is no reason to complicate the syntax just because in one other place (where you actually implement an interface) the distinction between classes and interfaces is relevant (for example if you implement an interface, you need to implement all methods it defines, but you don't need to, if you extend a (non-abstract) class).

Assuming for a moment that you'd have to write implements here, then you'd also need a separate syntax for enum values (instead of simply writing <E extends Enum<E>>) and annotations (which you can easily declare using <A extends Annotation>).

The only place where you need to write implements is at the point where you actually implement the interface. At that point (and that point only) the difference is important, because you must implement the methods defined in the interface. For everyone else, it doesn't matter if any given A is a base class or an implemented interface of B: it's a super-type and that's all that matters.

Also note that there's one other place where you use extends with interfaces:

interface A {
}

interface B extends A {
}

At this point implements would be wrong, because B does not implement A.

Joachim Sauer
  • 11,016
5

When Java 5, and in particular generics, was initially made available to developers who had registered an interest the syntax was quite different. Instead of Set<? extends Foo> and Set<? super Bar> it had Set<+Foo> and Set<-Foo>. However, the feedback was that it wasn't clear whether + meant more specific or wider (more classes). Sun responded to that feedback by changing the syntax, but within the constraint of not introducing new keywords, which would have been a problem for backwards compatibility.

The result is that neither is quite natural. As you observe, extends is overloaded to talk of classes "extending" interfaces, which isn't the language used in other contexts; and super is overloaded to mean "is a superclass of", which is the opposite direction of the relationship previously expressed by the keyword, i.e. referring to a superclass.

However, the fundamentals of what an interface is are not affected by this change, and it doesn't introduce special interfaces which are extended in a new way.

Peter Taylor
  • 4,043
2

There are disadvantages in both allowing and prohibiting such syntax, and those of allowing are far greater.

Just think of it.

Separation of interface and implementation is one of fundamental programming idioms. Because of that, syntax allowing to spell "interface implements something" would be about as bad as one using plus sign to denote multiplication of operands.

gnat
  • 20,543
  • 29
  • 115
  • 306
-1

One needs to understand interface driven programming as next step while understanding an interface. It tell what is actual use of interface. What role it plays in a Java (or any other language) program.

Deepak
  • 1