What will happen in Java when I use a method of the super class which has not been written?
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
}
}
It won't compile. If it was there at compile time but not at runtime, then it will throw an Error.
- If that's the specific situation you're asking about then it's always there.
- If you're asking in general then it won't compile.
There will always be an implementation in the super class.
JPanel
implements paintComponent()
. So, you don't need to worry about it.
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
精彩评论