1

I've been reading the Java 8 OCP book by Sybex and I can't seem to understand why the third line doesn't compile.

public static void main(String[] args) {
    List<? super IOException> exceptions = new ArrayList<Exception>();
     exceptions.add(new Exception()); // DOES NOT COMPILE
     exceptions.add(new IOException());
     exceptions.add(new FileNotFoundException());
}

1 Answers1

1

Exception doesn't inherit from IOException while IOException and FileNotFoundException does.

So the hierarchy is:

Exception
   IOException
       FileNotFoundException

So, FileNotFoundException is an IOException, but Exception is Not.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • 2
    I feel like this does little to answer the question. I believe the true problem here is the usage of `super`. And even then, [this question](http://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java) should answer that quite well. – Obicere Jun 01 '16 at 06:07