474 Object-Oriented Programming (Cheapest web hosting) Chapter 9 9.13 finalMethods and

474 Object-Oriented Programming Chapter 9 9.13 finalMethods and Classes We saw in Chapter 7 that variables can be declared final to indicate that they cannot be modified after they are declared and that they must be initialized when they are declared. It is also possible to define methods and classes with the final modifier. A method that is declared final cannot be overridden in a subclass. Methods that are declared static and methods that are declared private are implicitly final. Because a final method s definition can never change, the compiler can optimize the program by removing calls to final methods and replacing them with the expanded code of their definitions at each method call location a technique known as inlining the code. A class that is declared final cannot be a superclass (i.e., a class cannot inherit from a final class). All methods in a final class are implicitly final. Performance Tip 9.2 The compiler can decide to inline a final method call and will do so for small, simple final methods. Inlining does not violate encapsulation or information hiding (but does improve performance because it eliminates the overhead of making a method call). Software Engineering Observation 9.17 A class declared final cannot be extended, and every method in it is implicitly final. Software Engineering Observation 9.18 In the Java API, the vast majority of the thousands of classes are not declared final. This enables inheritance and polymorphic processing the fundamental capabilities of object- oriented programming. However, in some cases it is important to declare classes final typically for security1 or performance reasons. 9.14 Abstract Superclasses and Concrete Classes When we think of a class as a type, we assume that objects of that type will be instantiated. However, there are cases in which it is useful to define classes for which the programmer never intends to instantiate any objects. Such classes are called abstract classes. Because these are used as superclasses in inheritance situations, we will normally refer to them as abstract superclasses. No objects of abstract superclasses can be instantiated. The sole purpose of an abstract class is to provide an appropriate superclass from which other classes may inherit interface and/or implementation (we will see examples of each shortly). Classes from which objects can be instantiated are called concrete classes. 1. Class String is an example of a final class. This class cannot be extended, so programs that use Strings can rely on the functionality of String objects as specified in the Java API. Making the class finalalso prevents programmers from creating subclasses that might bypass security restrictions. For example, when a Java program attempts to open a file on your computer, the program supplies a String representing the name of the file. In many cases, opening a file is subject to security restrictions. If it were possible to create a subclass of String, that subclass might be implemented in a manner that enables it to specify one String to pass a security or permissions test, then specify a different name when the program actually opens the file. For more information on final classes and methods visit: java.sun.com/docs/books/tutorial/ java/javaOO/final.html [Note: Strings are covered in detail in Chapter 10 and file processing is covered in detail in Chapter 16.] Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply