Search code examples
javaexceptiontry-catchsubclasssuperclass

Java superclass catch and subclass catch


I am confused. Are the following two statements accurate?

  1. An object of a class has no knowledge of, or access to, any of its subclasses.

  2. On the topic of exception handling, a superclass catch will catch all of its subclasses.

I think there's some logic that I'm not seeing. In 1 the superclass has no knowledge of the subclass, whereas in 2 the superclass does have knowledge of the subclass. Any clarification on this would be appreciated.


Solution

  • The super class itself has no knowledge of its sub-classes, but the jvm has that knowledge, and jvm is doing the work for exception handling, polymorphism etc.