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

What is the formula to calculate percentage?

542


What are access specifiers available in java?

590


How do you do exponents in java?

558


What are the main differences between the java platform and other platforms?

535


What are methods in java?

546






What is the difference between double and float variables in java?

642


How to use arraylist in java netbeans?

518


Can we define package statement after import statement in java?

569


What are access specifiers in java ?

599


What is treeset and treemap in java?

589


What is the difference between a factory and abstract factory pattern?

689


Is minecraft 1.15 out?

559


What is an argument java?

543


What is class variable java?

594


What is finally block?

578