Page 523 - Introduction to Programming with Java: A Problem Solving Approach
P. 523

                Figure 12.13 Driver of constructors and methods in an inheritance hierarchy
hierarchy should be used. Ideally, you should just instantiate objects from classes at the bottom of the hierar- chy. Ideally, all the classes above the bottom classes are there to make the bottom classes simple. In real life, we often do use classes above the bottom, but using bottom classes only is the ideal situation.
12.8 The final Access Modifier
You’ve used the final access modifier for quite a while now to turn a variable into a named constant. In this section, you’ll learn how to use final to modify a method and modify a class.
If you use the final modifier in a method heading, you’ll prevent the method from being overridden with a new definition in a subclass. You might want to do this if you think that your method is perfect and you don’t want its original meaning to “drift.” You might also want to consider using final to help speed things up a bit. Methods that use the final modifier should run faster since the compiler can generate more efficient code for them. The code efficiency comes from the compiler not having to prepare for the possibil- ity of inheritance. However, the speed improvement is miniscule for adding final to a single method, and you probably won’t notice it unless you have a large programming project with lots of subclasses and you use final a lot.
If you use the final access modifer in a class heading, you prevent the class from having any sub- classes. You might want to do this if you have a class that’s good and reliable, and you want to preserve its quality and protect it from future “feature creep.” By the way, if a class is declared to be a final class, there’s no point in specifying final for any of its methods. A final class cannot be extended, so overrid- ing methods cannot exist.
12.8 The final Access Modifier 489
 /***************************************************************
*
FullTimeDriver.java
Dean & Dean
The describes a full-time employee.
***************************************************************/
*
*
*
public class FullTimeDriver
{
}
public static void main(String[] args)
{
}
FullTime fullTimer = new FullTime("Shreya", 5733, 80000);
fullTimer.display();
System.out.println(fullTimer.getName());
// end FullTimeDriver class
Output:
name: Shreya
id: 5733
salary: $80,000
Shreya
 Apago PDF Enhancer
 


































































   521   522   523   524   525