Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock test question

 
Prasad Ballari
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Following is the code from "John Hunt"
import java.applet.Applet;
import java.awt.*;

public class Sample extends Applet {
private String text = "Hello World";
public void init() {
add(new Label(text));
}
//1 from
public Sample (String string) {
text = string;
}
// 1 to
}
The program gives runtime error.Is it due to abesence of no-argument constructor as JVM calles no-argument constructor..

Program runs without error if you replace //1 from - //1 to by
public Sample(){
super();
}
Pls clarify my doubt..
prasad
------------------

[This message has been edited by Prasad Ballari (edited December 03, 2000).]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a special requirement for applets. It is the responsibility of the browser to create the applet object - this is done by calling the no-args constructor.
An applet constructor with an argument does not make any sense because the programmer has no control over how the constructor is called.
Bill
 
naveen sahu
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:
That is a special requirement for applets. It is the responsibility of the browser to create the applet object - this is done by calling the no-args constructor.
Bill

No Mr Brodgen this is not correct what are you saying.
you are wrong here. any of the browser didn't create thr applet object by calling no args constructor, inspite of it , it is done by init() method. which is having signature like
public void init()
as Mr Prasad Ballari all ready stated that providing an other constructor like
public Sample (String string) {
text = string;
}
didn't create any problum. and according to the rule of
java if any of the class having any constructor then default
constructor will not be provided. so at the presence of
above constructor default no args constructor can't exists.
and above all every browser having a JVM inside that so it has
to follow the java semantics and applet is not an exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic