Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Let's say I have an interface A with generic type:

public interface A<T> {
    T getT();
}

and a class B implementing it:

public class B implements A {
    public Integer getT() { return 1; }
}

...but without give it a type parameter.

What happens here? Does the A be infer to A<Integer>? Is it possible to force user to write type parameter with implements statement(like B implements A<Integer>)?

share|improve this question
    
@johnchen902 I thought you were going to break into a racist rant when I saw the "a" before "Chinese", before I realized you are Chinese :P –  christopher May 12 '13 at 7:39
    
@johnchen902 it's pretty off-topic :p But yes, Taiwanese precisely. In fact I went to the same high school as yours. –  Lai Yu-Hsuan May 12 '13 at 7:44
add comment

4 Answers

up vote 2 down vote accepted

Your Question:

What happens here?

Since you left out the generic type, the system inferred it to be Object. You could have done the following with the same results:

    public class B implements A<Object> { 
        ...
    }

Next Question:

Is it possible to force user to write type parameter with implements (like B implements A)

Yes. This is how:

    interface A<T extends Integer> 

Hope this helps.

share|improve this answer
2  
Final detail: public Integer getT() ... in the above, where T is Object, works because of covariant return types –  Ryan Stewart May 13 '13 at 1:01
add comment

Well, the "right" way to implement class B is:

public class B implements A<Integer> {
    @Override
    public Integer getT() {
        return 1;
    }
}

Once you are writing the class B this way you enforce user to use this class with Integer type only.

When the generics specifier is missing (as in your example) the code is compiled (because generics are erasures) but most IDEs produce warning (e.g. in Eclipse the warning is "A is a raw type. References to generic type A should be parameterized B.java"

share|improve this answer
add comment

What happens here?

Well, because you've left the interface implementation as the raw type, the return type must be of type Object. It doesn't infer the return type, but if your return type is of type Object, which is every single object in Java, then it will work fine.

Is it possible to force user to write type parameter with implements statement(like B implements A)?

Force the user to type it? Computers are powerful, but mind control is a little way off yet.

share|improve this answer
    
I don't want to control users' minds haha. What I meant is like that I can force users to give it a type extending Foo by interface A<T extends Foo>. –  Lai Yu-Hsuan May 12 '13 at 7:46
add comment

What happens here? Well,because Integer extends Object ,so it will work fine , suggest you add parameter with implements

share|improve this answer
    
"it will work fine" - but B is an A instead of an A<Integer>, which has some implications. –  Paul Bellora May 12 '13 at 15:26
    
change it to A<Object> –  duplicatedcode May 12 '13 at 16:16
    
Uh, I'm not the OP, but that doesn't answer "What happens here?" anyway. –  Paul Bellora May 12 '13 at 16:19
    
Uh,It is java "type erasure" , i want to write much ,but english is not good,so sorry ,you can google ,i think you can find truly –  duplicatedcode May 12 '13 at 16:32
add comment

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.