Faced with the phenomenon:

public class MenuIndexException extends ArrayIndexOutOfBoundsException { public MenuIndexException(String massage) { super(massage); } public void answer() { } } 

Why doesn't this record work? Polymorphism for exception classes does not work? Or am I somehow not applying it correctly?

 ArrayIndexOutOfBoundsException ex = new MenuIndexException("s"); ex.answer(); 

Do not want to compile the idea immediately underlines the red ... Why? although I equated to the ex = new MenuIndexException , I still do not have a method that is in this class ...

  • 2
    And it should not work. The list of available methods is determined by the type of pointer. - pavel
  • And yes that's it! And I thought the type of object is all clear thanks. - Pavel

1 answer 1

Working solution code:

 public class MenuIndexException extends ArrayIndexOutOfBoundsException { private static final long serialVersionUID = -505311792750028191L; public MenuIndexException(String massage) { super(massage); } public void answer() { } public static void main(String[] args) { final MenuIndexException ex = new MenuIndexException("s"); ex.answer(); } }