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 point of java?

790


What is callablestatement? How you can call stored procedure to pass in parameter?

772


What is a type parameter in java?

766


Can we initialize the final blank variable?

826


What is immutability in java?

827


How do you define a variable?

739


What about interrupt() method of thread class ?

850


Why do we use threads in java?

813


What is number data type?

777


What is the difference between and ?

706


Explain java coding standards for classes or java coding conventions for classes?

881


What is the difference between the final method and abstract method?

794


Explain the public class modifier?

720


Write a code to show a static variable?

846


what is the use of bean managed and container managed with example?

1745