Can Applet have constructors?

Answer Posted / sunil dhadwe

In Java, every class has constructor, so Applet class also has constructor which is invoked when Applet object is created by browser or Applet Viewer. Usually we don't need constructor as we don't create Applet object.
To write applet we write our own class that extends Applet, so our class also becomes Applet. Our class can also have constructor. From our class we can call super class (Applet) constructor. Look at following code:
import java.applet.Applet;
import java.awt.Graphics;

/*
<applet code="AppletConst" width=400 height=300>
</applet>
*/

public class AppletConst extends Applet {

public AppletConst() {
super(); // no compilation error : means Applet class has constructor
System.out.println("Applet constructor");
}

public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}

Is This Answer Correct ?    39 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Can we overload destructor in java?

757


what is difference between equals and ==?

799


What happens if we don’t override run method ?

763


What are the four integer types supported by java?

782


What is array initialization in java?

701


What is default locale java?

791


Tell me about your ability to work under pressure

1952


Can we call the run() method instead of start()?

793


Explain public static void main(string args[]) in java.

721


what do you mean by marker interface in java?

760


What are the types of strings?

779


What is difference between module and function?

770


Difference between static and dynamic class loading.

795


What is the parse method in java?

796


What is the class in java?

760