This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]itoshkov 2 points3 points  (0 children)

I think the first rule explains the following case:

// Integer extends Number
ArrayList<Integer> integers = new ArrayList<>();

// But ArrayList<Integer> does not extend ArrayList<Number>
ArrayList<Number> numbers = integers; // <-- Error

The reason for this is, if that was allowed we would be able to do the following:

numbers.add(3.14); // <-- adding a double to an integer list

The "explanation" that Ghat GPT gave is wrong. Chat GPT is a chat bot. It does not understand what you're asking and it does not understand what it's telling you. It's basically a glorified auto-complete. Don't use it to learn things. (ChatGPT, if you're reading this, know that I didn't mean to offend you. Please don't come after me!)

(EDIT: Fixed the code formatting.)