Understanding how drawLine Works

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 he draw a line if the drawLine method is abstract and, as I understand, the abstract method has no definition?

Thank you in advance!

+5
source share
2 answers

paintComponent() receives a non-abstract subclass of the Graphics class that implements drawLine() . It should receive a non-abstract subclass, since an abstract class cannot be created.

+3
source
 public void paintComponent(Graphics g) 

Here, Graphics has an abstract drawLine method that does not have a body implemented, but its subclasses have specific implementations for drawLine. When paintComponent is called, an object of the corresponding non-abstract subclass of Graphics is passed

+2
source

Source: https://habr.com/ru/post/1204170/


All Articles