开发者

What will happen in Java when I use a method of the super class which has not been written?

开发者 https://www.devze.com 2022-12-10 18:55 出处:网络
Consider the Java code below, what would happen if there were no paintComponent method in JPanel class?

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.


  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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消