Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 13 down vote accepted

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.

share|improve this answer
    
If we'll use your logics, we should use 'extends SomeInterface' always, not only in generics. –  Gangnus Feb 29 '12 at 9:02
    
@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. –  Joachim Sauer Feb 29 '12 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 '12 at 9:19
    
Thank you for the code example. So other cases exist. But this yours seems logical to me. On the other side, T is a class, not interface. And it implements. –  Gangnus Feb 29 '12 at 9:29
    
@Gangnus: who says that T is a class? T could reference an interface type or an enum type. –  Joachim Sauer Feb 29 '12 at 10:03

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.

share|improve this answer
    
+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 '12 at 13:23

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.

share|improve this answer
    
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 '12 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 '12 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 '12 at 11:51

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.

share|improve this answer
2  
How does your answer address the OP's concerns? Please edit your answer to be more clear. –  GlenH7 Jul 1 '13 at 14:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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