Author Archives: Linda Hammock

Final Methods in Classes A final method in a class is a concrete method (i.e., has an implementation) and cannot be overridden or hidden in any subclass. Any normal class can declare a final method. The class need not be declared final. In Example 5.9, the non-final class Light defines the final method setWatts() at (10). Click here to view code image class Light {                                    // (1)  // …  public final void setWatts(int watt) {         // (10) Final instance method    noOfWatts = watt;  }} The subclass TubeLight attempts to override the final method setWatts() from the superclass Light at (14), which is not permitted. Click here to view code image class TubeLight extends Light {                  // (12)  // …  @Override  public void setWatts(int watt) {   // (14) Cannot override final method at (10)!    noOfWatts = 2*watt;  }} A call to a final method is bound at compile time; as such, a…

Read more

The super() Constructor Call The constructor call super() is used in a subclass constructor to invoke a constructor in the direct superclass. This allows the subclass to influence the initialization of its inherited state when an object of the subclass is created. A super() call in the constructor of a subclass will result in the execution of the relevant constructor from the superclass, based on the signature of the call. Since the superclass name is known in the subclass declaration, the compiler can determine the superclass constructor invoked from the signature of the parameter list. A constructor in a subclass can access the class’s inherited members by their simple names. The keyword super can also be used in a subclass constructor to access inherited members via its superclass. One might be tempted to use the super keyword in a constructor to specify initial values for inherited fields. However, the super()…

Read more

Final Variables A final variable is a variable whose value cannot be changed during its lifetime once it has been initialized. A final variable is declared with the keyword final in its declaration. Any attempt to reassign a value will result in a compile-time error. Declaring a variable as final has the following implications: For all paths of execution through the code, the compiler checks that a final variable is assigned only once before it is accessed, and when in doubt, the compiler issues an error (p. 232). A blank final variable is a final variable whose declaration does not specify an initializer expression. A constant variable is a final variable of either a primitive type or type String that is initialized with a constant expression. Constant variables can be used as case labels of a switch statement. We distinguish between two kinds of final variables: final fields that are…

Read more

Abstract Methods in Classes In this subsection we discuss in more detail declaring and overriding abstract methods in classes. Declaring an abstract Method An abstract method in an abstract class has the following syntax: Click here to view code image access_modifier abstractreturn_type method_name (formal_parameter_list)                                      throws_clause; An abstract method does not have an implementation; that is, no method body is defined for an abstract method, and only the method header is provided in the class declaration. The keyword abstract is mandatory in the header of an abstract method declared in a class. Its class is then incomplete and must be explicitly declared as abstract. Subclasses of an abstract class must then override the abstract method to provide the method implementation; otherwise, they must also be declared as abstract. Overriding an abstract Method When overriding an abstract method from the superclass, the notation @Override should always be used in the overriding method in the subclass.…

Read more

Final Fields in Classes A final field must be explicitly initialized only once with an initializer expression, either in its declaration or in an initializer block. A final instance field can also be initialized in a constructor. After the class completes initialization, the final static fields of the class are all guaranteed to be initialized. After a constructor completes execution, the final instance fields of the current object are all guaranteed to be initialized. The compiler ensures that the class provides the appropriate code to initialize the final fields. In Example 5.9, the class Light defines two final static fields at (2) and (3). The field KWH_PRICE is a constant variable that is initialized by the constant expression 3.25 in the declaration at (2). The field MANUFACTURER is a blank final static field that is initialized in the static initializer block at (4). All static initializer blocks are executed at…

Read more

Extending an abstract Class A class might choose the design strategy with abstract methods to dictate certain behavior, but allow its subclasses the freedom to provide the relevant implementation. An abstract class forces its subclasses to provide the subclass-specific functionality stipulated by its abstract methods, which is needed to fully implement its abstraction. In other words, subclasses of the abstract class have to take a stand and provide implementations of any inherited abstract methods before objects can be created. In Example 5.8, since the class Light is abstract, it forces its concrete (i.e., non-abstract) subclass to provide an implementation for the abstract method energyCost(). The concrete subclass TubeLight provides an implementation for this method at (3). Click here to view code image class TubeLight extends Light {  // …  // Implementation of the abstract method from the superclass.  @Override public double energyCost(int noOfHours) {      // (3)    return  0.15 * noOfHours; …

Read more

Final Local Variables in Methods A final local variable need not be initialized in its declaration—that is, it can be a blank final local variable—but it must be initialized in the code before it is accessed. However, the compiler does not complain as long as the local variable is not accessed—this is in contrast to final fields that must be explicitly assigned a value, whether they are accessed or not. In Example 5.9, the main() method at (15) in the class Warehouse defines a final local reference workLight at (16). The state of the object denoted by the reference workLight is changed at (17), but an attempt to change the value of the final instance field color of this object at (18) does not succeed. The compiler also reports an error at (19), since the reference value of the final local variable work-Light cannot be changed. A blank final local…

Read more

Definite Assignment Analysis for Final Variables The name of a final variable can occur in the following contexts: in its declaration when it is declared, in the context of an assignment when it is assigned a value, and in the context where its value is accessed in an expression. The analysis performed by the compiler determines whether the blank final variable is initialized before its value is accessed. This involves checking whether a blank final variable has been assigned a value on any possible path of execution to where the value of the variable is accessed. In technical terms, this means a blank final variable must be definitely assigned before any access. Click here to view code image final int k;              // Declaration: blank final local variablek = 10;                   // (1) AssignmentSystem.out.println(k);    // (2) Access: k is definitely assigned. boolean status = true;    // Non-constant variable.final int j;              // Declaration:…

Read more

5.5 Final Declarations The keyword final can be used in the following contexts in Java: Final Classes A class can be declared final to indicate that it cannot be extended—that is, one cannot define subclasses of a final class. In other words, the class behavior cannot be changed by extending the class. This implies that one cannot override or hide any methods declared in such a class. Its methods behave as final methods (p. 226). A final class marks the lower boundary of its implementation inheritance hierarchy. A concrete class is a class that has only concrete methods—that is, methods that are non-abstract, and therefore have an implementation. Only a concrete class can be declared final. If it is decided that the class TubeLight at (12) in Example 5.9 may not be extended, it can be declared final. Any attempt to specify the class name TubeLight in an extends clause…

Read more

5.6 Interfaces An interface defines a contract for services that classes can implement. Objects of such classes guarantee that this contract will be honored. Before diving into interfaces, an overview of the inheritance relationship between classes can be useful. The extends clause in a class definition only allows linear inheritance between classes—that is, a subclass can only extend one superclass. A superclass reference can refer to objects of its own type and of its subclasses strictly according to the linear inheritance hierarchy. Note that this inheritance relationship between classes comprises both inheritance of type (i.e., a subclass inherits the type of its superclass and can act as such) and inheritance of implementation (i.e., a subclass inherits methods and fields from its superclass). Since this relationship is linear, it rules out multiple inheritance of implementation, in which a subclass can inherit implementation directly from more than one direct superclass. As we…

Read more

20/30