3

There are 2 interfaces:

public interface A {
    ArrayList<Integer> all() throws ExceptionA;
}

public interface B {
    List<String> all() throws ExceptionB;
}

And there is class that implements them:

public class C implements A, B {
    @Override
    public ArrayList all() {
        return null;
    }
}

Also checked exceptions:

public class ExceptionA extends Exception {
}


public class ExceptionB extends Exception {
}

When I compile this code everything is OK. Why?

I know that in implementation we can use subtype (in this situation ArrayList is subtype of List). But I have next questions:

  1. Why in implementation you can omit throws part? In interface methods there are throws parts of checked exceptions
  2. Why in implementation you can omit generic type?
Iakov Burtsev
  • 377
  • 1
  • 2
  • 15
  • 5
    1. [Java interface throws an exception but interface implementation does not throw an exception?](https://stackoverflow.com/questions/15607060/java-interface-throws-an-exception-but-interface-implementation-does-not-throw-a) – Zabuzard Dec 07 '18 at 16:16
  • 5
    2. https://stackoverflow.com/q/2770321/3788176 you can, but you shouldn't. – Andy Turner Dec 07 '18 at 16:18
  • 2
    2. because of backwards compatibility – Ulad Dec 07 '18 at 16:20

0 Answers0