Live data from Hacker News

Why do we need "? extends" in Java

stackoverflow.com

1–10 of 28 posts

Re: Why do we need "? extends" in Java

#7
post #4

Wow, I'm so glad that I don't need to worry about such a mess when using duck-typing in Python.

I'm glad that when I refactor code in Java, the type checker catches many of my mistakes. Not as many as in Haskell, but still.

In Python, the deploy/run unit tests/spot typo cycle greatly slows me down.

Re: Why do we need "? extends" in Java

#8
We had to learn it when doing an oo programming course at university. And I immediately forgot all about it.

I hope when writing Java code sometimes at a job, I can refer to this again.

This is all done in the name of type safe programming (I don't know the exact english terminology).

is called covariance. It helps to access attributes of B and derived classes with get() but prevents to use set().

ArrayList list;

list = new ArrayList();

list = new ArrayList();

list = new ArrayList();

list.set(index, myInteger); // throws error

Compiler has to know the exact type to set the value, but Long, Double and Integer are all from type Number, so you can read it into a Number variable.

Is that correct?

Post reply on HN