Chapter 6 Methods 291 8 public class MethodOverload (Web design rates)

Chapter 6 Methods 291 8 public class MethodOverload extends JApplet { 9 10 // first definition of method square with double argument 11 public int square( double x ) 12 { 13 return x * x; 14 } 15 16 // second definition of method square with double argument 17 // causes syntax error 18 public double square( double y ) 19 { 20 return y * y; 21 } 22 23 } // end class MethodOverload MethodOverload.java:18: square(double) is already defined in MethodOverload public double square( double y ) ^ MethodOverload.java:13: possible loss of precision found : double required: int return x * x; ^ 2 errors Fig. 6.17Compiler error messages generated from overloaded methods with Fig. 6.17 identical parameter lists and different return types (part 2 of 2). 6.16 Methods of Class JApplet We have written many applets to this point in the text, but we have not yet discussed the key methods of class JApplet that the applet container calls during the execution of an applet. Figure 6.18 lists the key methods of class JApplet, specifies when they get called and explains the purpose of each method. These JApplet methods are defined by the Java API to do nothing unless you provide a definition in your applet s class definition. If you would like to use one of these methods in an applet you are defining, you must define the first line of each method as shown in Fig. 6.18. Otherwise, the applet container will not call your versions of the methods during the applet s execution. Defining the methods as discussed here is known as overriding the original method definition. The applet container will call the overridden version of a method for your applet before it attempts to call the default versions inherited from JApplet. Overriding is discussed in detail in Chapter 9. Common Programming Error 6.18 Providing a definition for one of the JApplet methods init, start, paint, stop or destroy that does not match the method headers shown in Figure 6.18 results in a method that will not be called automatically during execution of the applet. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Leave a Reply