Here’s an example:
// set up frame, panel
JFrame container = new JFrame("Game Example");
panel = (JPanel)container.getContentPane();
I checked the Java API and both classes implement the same interface so is that and polymorphism the reason why?
Or is it because both classes inherit from the Component class through inheritance?
It’s that the contentPane usually is a JPanel. Print out the object’s class name and see for yourself.
And note that you are not in fact “typecasting a JFrame into a JPanel”. The JFrame’s contentPane is not the JFrame itself, not hardly.