views:

180

answers:

4

Consider the Java code below, what would happen if there were no paintComponent method in JPanel class?

...

import javax.swing.JPanel;

public class ShapesJPanel extends JPanel
{

     public void paintComponent( Graphics g )
     {
       super.paintComponent( g );
       //more codes here  
     }
}
+9  A: 

It won't compile. If it was there at compile time but not at runtime, then it will throw an Error.

Yishai
Is there a situation I'm not aware of where it would not be there at runtime? The compiler can, must and will statically check this, so there's no option to have it 'not be there' after a successful compile (or am I missing something)?
Michael Burr
Compile the ShapesJPanel class but deploy it with a different version of JPanel class which doe not have this method.
matt b
Thank you matt, exactly my point.
Yishai
OK, that makes sense.
Michael Burr
+1  A: 

There will always be an implementation in the super class.

JPanel implements paintComponent(). So, you don't need to worry about it.

jjnguy
+3  A: 
  1. If that's the specific situation you're asking about then it's always there.
  2. If you're asking in general then it won't compile.
Leo Jweda
A: 

At compile time

  • The method should present in any of the class in super class hierarchy otherwise compilation will fail

  • The method of the class from that hierarchy ( starting from bottom) will get called

At run time

  • If method does not found it will throw an error
JRomio