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

How can we achieve thread safety in java?

679


Why are constructors used?

550


what do you understand by the term string with respect to java?

524


Can we serialize arraylist in java?

532


Is space a character in java?

509






How do you add an element to an arraylist in java?

481


What is class array in java?

503


How to create an interface?

623


Explain importance of throws keyword in java?

560


What is size () in java?

540


What is tree in java?

484


What is use of super keyword in java?

592


What is a copy constructor in java?

572


Can we instantiate interface in java?

667


How can you set the applet size?

567