Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 4 Advanced Class Design

An abstract class need not declare an abstract method, which means it is not necessary for an abstract class to have methods declared as abstract. However, if a class has an abstract method, it should be declared as an abstract class.

A subclass of an abstract class needs to provide implementation of all the abstract methods; otherwise you need to declare that subclass as an abstract class.

An abstract class may have methods or fields declared static.

Using the “final” Keyword

For the OCPJP 7 exam, you need to know the uses of the final keyword. In this section, you’ll learn how to use the final keyword with classes, methods, and variables.

Final Classes

A final class is a non-inheritable class—that is to say, if you declare a class as final, you cannot subclass it. In general, OOP suggests that a class should be open for extension but closed for modification (Open/Closed Principle). However, in some cases you don’t want to allow a class to be subclassed. Two important reasons are

1.To prevent a behavior change by subclassing. In some cases, you may think that the implementation of the class is complete and should not change. If overriding is allowed, then the behavior of methods might be changed. You know that a derived object can be used where a base class object is required, and you may not prefer it in some cases.

By making a class final, the users of the class are assured the unchanged behavior.

2.Improved performance. All method calls of a final class can be resolved at compile time itself. As there is no possibility of overriding the methods, it is not necessary to resolve the actual call at runtime for final classes, which translates to improved performance. For the same reason, final classes encourage the inlining of methods. If the calls are to be resolved at runtime, they cannot be inlined.

In the Java library, many classes are declared as final; for example, the String (java.lang.String) and System (java.lang.System) classes. These classes are used extensively in almost all Java programs. For example, if you use a System.out.println() statement, you are using both the System class (in which the output stream and println methods are present) as well as the String class since println takes String as an argument. If these two classes are not declared final, it is possible for someone to change the behavior of these classes by subclassing and then the whole program can start behaving differently. To avoid such a problem, widely used classes like these and wrapper classes such as Integer are made final in the Java library.

The performance gain from making a class final is modest; the focus should be on using final where it is appropriate. The OCPJP 7 exam will mainly check whether you know the correct uses of the final keyword.

In the FunPaint example, you have a canvas for dragging and dropping shapes to create pictures. Assume that you have a Canvas class for implementing that functionality. Further, you want to ensure that the behavior does not change by inheriting from Canvas. In other words, you want to make Canvas a final class.

90

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]