382 Object-Based Programming Chapter 8 Software Engineering Observation (Hp web site)
382 Object-Based Programming Chapter 8 Software Engineering Observation 8.2 Class definitions that begin with keyword public must be stored in a file that has exactly the same name as the class and ends with the .java file name extension. Common Programming Error 8.1 Defining more than one public class in the same file is a syntax error. Figure 8.1 contains a simple definition for class Time1. Our Time1 class definition begins with line 6, which indicates that class Time1extends class Object (from package java.lang). Remember that you never create a class definition from scratch. In fact, when you create a class definition, you always use pieces of an existing class definition. Java uses inheritance to create new classes from existing class definitions. Keyword extends followed by class name Object indicates the class (in this case Time1) from which our new class inherits existing pieces. In this inheritance relationship, Object is called the superclass or base class and Time1 is called the subclass or derived class. Using inheritance results in a new class definition that has the attributes (data) and behaviors (methods) of class Object as well as new features we add in our Time1class definition. Every class in Java is a subclass of Object (directly or indirectly). Therefore, every class inherits the 11 methods defined by class Object. One key Object method is toString, discussed later in this section. Other methods of class Object are discussed as they are needed throughout the text. For a complete list of class Object s methods, see the online API documentation at java.sun.com/j2se/1.3/docs/api/index.html Software Engineering Observation 8.3 Every class defined in Java must extend another class. If a class does not explicitly use the keyword extends in its definition, the class implicitly extends Object. The body of the class definition is delineated with left and right braces ({ and }) on lines 7 and 50. Class Time1 contains three integer instance variables hour, minute and second that represent the time in universal-time format (24-hour clock format). Keywords public and private are member access modifiers. Instance variables or methods declared with member access modifier public are accessible wherever the program has a reference to a Time1 object. Instance variables or methods declared with member access modifier private are accessible only to methods of the class in which they are defined. Every instance variable or method definition should be preceded by a member access modifier. The three integer instance variables hour, minute and second are each declared (lines 8 10) with member access modifier private, indicating that these instance variables are accessible only to methods of the class. When a program creates (instantiates) an object of the class, such instance variables are encapsulated in the object and can be accessed only through methods of that object s class (normally through the class s public methods). Typically, instance variables are declared private, and methods are declared public. It is possible to have private methods and publicdata, as we will see later. The private methods are known as utility methods or helper methods because they can be called only by other methods of that class and are used to support the operation of those methods. Using public data is uncommon and is a dangerous programming practice. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services