Reputation: 33
In java Final classes can't be extended by normal classes but a generic type can be extended. What does this signify? For example Integer is a final class in JAVA API this class cant be extended but the following code does not show any error.
public class SampleClass<T extends Integer> {
// memebers
}
Upvotes: 0
Views: 731
Reputation: 25725
That is because the compiler expects T
to be of type Integer
or a type that derives from Integer
.
Since Integer
is final
class, T
can only be Integer
.
The error can only happen when you're trying to create a type from Integer
for example, the compiler will throw an error here:
class IncompilableInteger extends Integer { }
because it is trying to create a type from Integer
, but this won't:
class Compilable <T extends Integer> { }
because it is trying to create a type that contains object(s) of type - either is an Integer
or a type that derives from Integer
but not a different Integer
by itself.
Upvotes: 1
Reputation: 234847
Although the code shows no errors, any actual binding of the type parameter T
will have to be to an Integer
since, as you correctly note, Integer
is a final
class. The generic type parameter, while it uses the extends
keyword, is not defining an actual class; hence there is no error in the code you posted.
Upvotes: 4