javainterfacecastingconcreteclass

Concrete Class implements a interface. Typecasted to Interface later. How does JVM recognize the Typecasted instance?


A Concrete class typecasted to interface, does JVM still consider it a instance of concrete class ?


Solution

  • When you create an assignment, the type of the assignment is inferred based on the type given. E.g. MyInterface mi = new MyImplementation(); mi in this case is typed as the interface, but refers to the implementation. The JVM will know that the instance points to an implementation if you do instance of.