Chapter 3 Introduction to Java (Domain and web hosting) Applets 115 As

Chapter 3 Introduction to Java Applets 115 As with applications, every Java applet you create contains at least one class definition. One key feature of class definitions that was not mentioned in Chapter 2 is that programmers rarely create class definitions from scratch. In fact, when you create a class definition, you normally use pieces of an existing class definition. Java uses inheritance (introduced in Section 1.15 and discussed in detail in Chapter 9, Object-Oriented Programming ) to create new classes from existing class definitions. Line 10 public class WelcomeApplet extends JApplet { begins a class definition for class WelcomeApplet. At the end of line 10, the left brace, {, begins the body of the class definition. The corresponding right brace, }, on line 23 ends the class definition. Keyword class introduces the class definition. Welcome- Applet is the class name. Keyword extends indicates that class WelcomeAppletinherits existing pieces from another class. The class from which WelcomeApplet inherits (JApplet) appears to the right of extends. In this inheritance relationship, JApplet is called the superclass or base class and WelcomeApplet is called the subclass or derived class. Using inheritance here results in a WelcomeAppletclass definition that has the attributes (data) and behaviors (methods) of class JApplet as well as the new features we are adding in our WelcomeApplet class definition (specifically, the ability to draw WelcometoJavaProgramming! on the applet). A key benefit of extending class JApplet is that someone else previously defined what it means to be an applet. The appletviewer and World Wide Web browsers that support applets expect every Java applet to have certain capabilities (attributes and behaviors). Class JApplet already provides all those capabilities programmers do not need to reinvent the wheel and define all these capabilities on their own. In fact, applet containers expect applets to have over 200 different methods. In our programs to this point, we defined one method in each program. If we had to define over 200 methods just to display WelcometoJavaProgramming!, we would never create an applet, because it would take too long to define one! Using extends to inherit from class JApplet enables applet programmers to create new applets quickly. The inheritance mechanism is easy to use; the programmer does not need to know every detail of class JApplet or any other superclass from which a new class inherits. The programmer needs to know only that class JApplet defines the capabilities required to create the minimum applet. However, to make the best use of any class, programmers should study all the capabilities of the superclass. Good Programming Practice 3.1 Investigate the capabilities of any class in the Java API documentation (java.sun.com/ j2se/1.3/docs/api/index.html) carefully before inheriting a subclass from it. This helps ensure that the programmer does not unintentionally reinvent the wheel by redefining a capability that the superclass already provides. Classes are used as templates or blueprints to instantiate (or create) objects for use in a program. An object (or instance) resides in the computer s memory and contains information used by the program. The term object normally implies that attributes (data) and behaviors (methods) are associated with the object. The object s methods use the attributes to provide useful services to the client of the object (i.e., the code in a program that calls the methods). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply