English 中文(简体)
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  
     }
}
最佳回答
  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.
问题回答

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

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签