Given the following code:
import javax.swing.*;
import java.awt.*;
public class NewClass extends JPanel {
public void paintComponent(Graphics g) {
g.drawLine(0, 0, 90, 90);
}
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.add(new NewClass());
jf.setSize(500, 500);
jf.setVisible(true);
}
}
Why does it draw a line if the method drawLine
is abstract and, as I managed to understand, an abstract method has no definition?
Thank you in advance!
paintComponent()
gets a non-abstract sub-class of Graphics
, which implements drawLine()
. It must get a non-abstract sub-class, since an abstract class cannot be instantiated.
public void paintComponent(Graphics g)
Here Graphics have abstract method drawLine that does not have a body implemented but its subclasses have concrete implementations for drawLine. When paintComponent is called, object of appropriate non-abstract subclass of Graphics is passed
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With