The for(:) Statement – Control Flow
4.8 The for(:) Statement The enhanced for loop is convenient when we need to iterate over an array or a collection, especially when some operation needs to be performed on each element of the array or collection. In this section we discuss iterating over arrays. In §15.2, p. 795, we take a look at the for(:) loop for iterating over collections. Earlier in this chapter we used a for(;;) loop to sum the values of elements in an int array: Click here to view code image int sum = 0;int[] intArray = {12, 23, 5, 7, 19};for (int index = 0; index < intArray.length; index++) { // (1) using for(;;) loop sum += intArray[index];} The for(;;) loop at (1) is rewritten using the for(:) loop in Figure 4.9. Figure 4.9 Enhanced for Statement The body of the loop is executed for each element in the array, where the variable element…
Extending Interfaces – Object-Oriented Programming
Extending Interfaces An interface can extend other interfaces, using the extends clause. Unlike when extending classes, an interface can extend several interfaces. The interfaces extended by an interface (directly or indirectly) are called superinterfaces. Conversely, the interface is a subinterface of its superinterfaces. Since interfaces define new reference types, superinterfaces and subinterfaces are also supertypes and subtypes, respectively. A subinterface inherits from its superinterfaces all members of those superinterfaces, except for the following: Barring any conflicts, a subinterface inherits abstract and default method declarations that are not overridden, as well as constants and static member types that it does not hide in its superinterfaces. In addition, abstract, static, and default method declarations can be overloaded, analogous to method overloading in classes. For a detailed discussion of overriding abstract methods from multiple superinterfaces, see §11.12, p. 621. Example 5.10 illustrates the relationships between classes and interfaces. In Example 5.10, the interface…
The Object Reference super – Object-Oriented Programming
5.2 The Object Reference super The this reference can be used in non-static code to refer to the current object (§3.5, p. 106). The keyword super, in contrast, can be used in non-static code to access fields and invoke methods from the superclass. The keyword super provides a reference to the current object as an instance of its superclass. In method invocations with super, the method from the superclass is invoked regardless of what the actual type of the current object is or whether the current class overrides the method. This approach is typically used to invoke methods that are overridden and to access members that are hidden to the subclass. Unlike the this keyword, the super keyword cannot be used as an ordinary reference. For example, it cannot be assigned to other references or cast to other reference types. Example 5.4 uses the superclass Light and its subclass TubeLight…
Final Methods in Classes – Object-Oriented Programming
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…
Abstract Methods in Classes – Object-Oriented Programming
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.…
Final Fields in Classes – Object-Oriented Programming
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…
Final Local Variables in Methods – Object-Oriented Programming
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…
Interfaces – Object-Oriented Programming
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…
Relationships: is-a and has-a – Object-Oriented Programming
Relationships: is-a and has-a The inheritance relationship between a subclass and its superclass is embodied by the is-a relationship. Since a subclass inherits from its superclass, a subclass object is-a superclass object, and can be used wherever an object of the superclass can be used. It has particular consequences for how objects can be used. An object of the TubeLight class is-an object of the superclass Light. Referring to Figure 5.1, an object of the TubeLight class can be used wherever an object of the superclass Light can be used. The inheritance relationship is transitive: If class B extends class A and class C extends class B, then class C will also inherit from class A via class B. In Figure 5.1, an object of the SpotLightBulb class is-an object of the class Light. The is-a relationship does not hold between peer classes: An object of the LightBulb class is…
Overriding Instance Methods – Object-Oriented Programming
Overriding Instance Methods Under certain circumstances, a subclass can override instance methods from its superclass. Overriding such a method allows the subclass to provide its own implementation of the method. The overridden method in the superclass is not inherited by the subclass. When the method is invoked on an object of the subclass, it is the method implementation in the subclass that is executed. The new method in the subclass must abide by the following rules of method overriding: Whether parameters in the overriding method should be final is at the discretion of the subclass. A method’s signature does not comprise the final modifier of parameters, only their types and order. The criteria for overriding methods also apply to interfaces, where a subinterface can override abstract and default method declarations from its superinterfaces (p. 237). Figure 5.2 Inheritance Hierarchy for Example 5.2 The canonical examples of method overriding in Java…