Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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!

like image 612
Andrei Avatar asked Oct 07 '14 12:10

Andrei


2 Answers

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.

like image 91
Eran Avatar answered Sep 30 '22 20:09

Eran


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

like image 39
Dr. Debasish Jana Avatar answered Sep 30 '22 20:09

Dr. Debasish Jana