What is use of super.paint(g)?

unknownerror picture unknownerror · May 2, 2014 · Viewed 12.1k times · Source

Can someone explain me what is the use of super.paint(g) where, g is a Graphics variable in Applets or awt or swings or in Java.

I have done research and found that it is used to override but what is the use of this override?

I am a beginner. If possible can you explain the difference between paint(g) and super.paint(g) with a small example or please help me with this code?

/*
Let us consider this code 
This has only one paint declaration i.e; subclass's paint method declaration, no     declaration for superclass's paint function... when we explicitly call superclass's paint function 
what is the use of super.paint(g) and is it going to use superclass's paint declaration??
*/

import java.awt.*;
import java.applet.*;
/*
<applet code="superpaintDemo" height=768 width=1366>
</applet>
*/
class superpaintDemo extends Applet
{

    public void paint(Graphics g)
    {
        super.paint(g);
        g.drawString("This is Demo",200,200);
    }
}

Answer

Jeroen Vannevel picture Jeroen Vannevel · May 2, 2014

public void paint(Graphics g)

Paints the container. This forwards the paint to any lightweight components that are children of this container. If this method is reimplemented, super.paint(g) should be called so that lightweight components are properly rendered. If a child component is entirely clipped by the current clipping setting in g, paint() will not be forwarded to that child.

Straight from the docs.