Search code examples
javaeclipseappletawtoverriding

When Overriding a method, don't we override whole method? I tried to override java.awt.Container.paint


I have a question about overriding. I thought that overriding a method is re-writing that method. But, while I was studying about how to override, I got a doubt. Suppose that I have written following code:

import java.applet.Applet;

import java.awt.Graphics;

public class guitryings_2 extends Applet{

    public void paint( Graphics g )
    {
        g.drawString("Hi", 25, 75);
        showStatus("Deneme");
    }

}

If the paint method includes only g.drawString("Hi",25,75); and showStatus doesn't exist, I get a message 'Applet Started'.

If override means re-writing whole the method, when I write only g.drawStrings without, it means I didn't define any String that will be showed below. So shouldn't 'Applet Started' text disappear?


Solution

  • Effectively, you are overriding java.awt.Container.paint(Graphics g) since neither Applet nor any class in the hierarchy up to Container provide their own implementation of paint().

    This is the method you are overriding (and, yes, methods are always overridden as a whole - you can not override part of a method only. The only thing you can do is to call the overridden method, like super.paint(g)).

    However, java.awt.Container.paint() does not contain any Applet specific code, hence the message which you see (Applet started) is obviously printed by the Applet container - like the browser (which still has the main control over its status bar), or the Applet Viewer.

    On a side remark, note that Applets are being deprecated and some browsers already decommissioned the required native APIs to support Applets at all.