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?

4 Answers 4

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.

17
  • If we'll use your logics, we should use 'extends SomeInterface' always, not only in generics.
    – Gangnus
    Feb 29, 2012 at 9:02
  • 2
    @Gangnus: no, because for the implementor there is a concrete difference between extends and implements, it's just when looking at the issue from a pure type-system perspective, that there is no difference. Feb 29, 2012 at 9:05
  • 1
    Sorry, I do not understand your thought. --1. Why should we use "a pure type-system perspective" in one case and not in the other? --2. Why the syntactic demands change in these different places? Could you explain it, please. In the answer, if possible.
    – Gangnus
    Feb 29, 2012 at 9:19
  • 1
    @Gangnus: who says that T is a class? T could reference an interface type or an enum type. Feb 29, 2012 at 10:03
  • 1
    coming from C#, this whole discussion is ridiculous. we denote a type's supertypes with :. and under the covers, an interface is and has always been an abstract class with the constraint of only containing unimplemented virtual (abstract) members, so as to allow for multiple inheritence. there is literally no difference between implements and extends. they could both be replaced with the same word (extends) or by some arbitrary symbol (:) and nothing would be lost.
    – sara
    May 20, 2016 at 18:15
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.

1
  • +1. I see and agree. But Joachim Sauer has found my erroneous thinking that T is necessarily a class. So, the answer is his. Your understanding is one(or 2) storey higher :-). My problem was more primitive. Thank you anyway for my development.
    – Gangnus
    Feb 29, 2012 at 13:23
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.

3
  • Ok. So, I really do not understand something. I considered it very probable. But you haven' explained the problem at all, sorry. T is a representation of a class. Why in one place I use T extends and in another T implements?
    – Gangnus
    Feb 29, 2012 at 11:37
  • @Gangnus in the example you provided T refers to type parameter which is not necessarily a class - see what Joachim told you already. Allowing implements for it would lead to the same mess I mentioned
    – gnat
    Feb 29, 2012 at 11:48
  • Yes, I have seen already his comment. It will be marked as the answer when put in answer. Till that you both have my thanks and +1.
    – Gangnus
    Feb 29, 2012 at 11:51
-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.

1
  • 2
    How does your answer address the OP's concerns? Please edit your answer to be more clear.
    – user53019
    Jul 1, 2013 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.