LuminousNutria
LuminousNutria

Reputation: 2001

Can `public <T extends Number> T method( Integer x ) {...}` take input of a type that extends Integer?

I am confused by one of the questions on my practice exam.

In Java, the Integer class extends the base abstract class Number. Now, suppose we have a method with a signature as follows:

public <T extends Number> T method( Integer x ) {...}

For each of the following, write “can” or “cannot” into the blanks to correctly complete the statement.

The method ______ take any input of a type that extends Integer

I am confused because when I try to make my own class that extends Integer, IntelliJ tells me, "Cannot inherit from final 'java.lang.Integer'".

If I can't inherit from the Integer class, then how would I give input that "extends Integer" in the first place?

Upvotes: 1

Views: 175

Answers (3)

mamian
mamian

Reputation: 66

The Integer class for Java is final, it can't be inherit.

public final class Integer extends Number implements Comparable<Integer> 

Upvotes: 0

howie
howie

Reputation: 2685

You should look into javadoc. You can't extend a class that is declared final

Upvotes: 0

LppEdd
LppEdd

Reputation: 21124

That statement is pretty badly formulated. I hope it was written like that for confusing the student, and not because they forgot primitive types' wrappers are final classes.

So, to answer your question, the answer would be "cannot".
But if you can, clarify this point with your professor or examiner.

Upvotes: 2

Related Questions