Category: Other Aspects of Record Classes
-
Constants in Interfaces – Object-Oriented Programming
Constants in Interfaces A field declaration in an interface defines a named constant. Naming conventions recommend using uppercase letters, with multiple words in the name being separated by underscores. Such constants are considered to be public, static, and final. These modifiers are usually omitted from the declaration, but can be specified in any order. Such…
-
Compact Canonical Record Constructor – Object-Oriented Programming
Compact Canonical Record Constructor The compact canonical record constructor is a more concise form of the normal canonical record constructor. It eliminates the need for a parameter list and the initialization of the component fields. The parameter list is derived from the component list declared in the record header, and the initialization of all component…
-
Sealed Classes and Interfaces 2 – Object-Oriented Programming
A sealed class is declared using the modifier sealed and the optional permits clause in the class header. The permits clause is optional if a sealed class and its permitted direct subclasses are declared in the same compilation unit (p. 317). If a class specifies the permits clause, the class must be declared sealed. The…
-
Private Methods in Interfaces – Object-Oriented Programming
Private Methods in Interfaces Private methods in interfaces are no different from private methods in classes. As such, they can only be accessed inside the interface, acting as helper or auxiliary methods for non-abstract methods declared in the interface. They allow code to be shared between the non-abstract methods in the interface, thus promoting code…
-
Array Store Check – Object-Oriented Programming
Array Store Check An array reference exhibits polymorphic behavior like any other reference, subject to its location in the type hierarchy (p. 278). However, a runtime check is necessary when objects are inserted in an array, as illustrated below. The following assignment is valid, as a supertype reference (Stack[]) can refer to objects of its…
-
Reference Value Assignment Conversions – Object-Oriented Programming
5.9 Reference Value Assignment Conversions In the context of assignments, the following conversions are permitted (Table 2.17, p. 47): In addition, for assignment conversions only, the following conversion is also possible: Note that these rules imply that a widening conversion cannot be followed by any boxing conversion, but the converse is permitted. Widening reference conversions…
-
The instanceof Type Comparison Operator 2 – Object-Oriented Programming
As we have seen, the instanceof type comparison operator effectively determines whether the reference value in the reference on the left-hand side refers to an object whose class is a subtype of the type specified on the right-hand side. At runtime, it is the type of the actual object denoted by the reference on the…
-
The instanceof Pattern Match Operator – Object-Oriented Programming
The instanceof Pattern Match Operator The instanceof operator can also be used for pattern matching, as explained below. It has the following syntax when used as a pattern match operator: Note that the syntax of the instanceof pattern match operator augments the syntax of the instanceof type comparison operator with a pattern variable. The type…
-
Implicit Static Methods for Enum Types – Object-Oriented Programming
Implicit Static Methods for Enum Types All enum types implicitly have the following static methods, and methods with these names cannot be declared in an enum type declaration: Click here to view code image staticEnumTypeName[] values() Returns an array containing the enum constants of this enum type, in the order they are specified. Click here…
-
Extending Enum Types: Constant-Specific Class Bodies – Object-Oriented Programming
Extending Enum Types: Constant-Specific Class Bodies Constant-specific class bodies define anonymous classes (§9.6, p. 521) inside an enum type—they implicitly extend the enclosing enum type creating new subtypes. The enum type Meal in Example 5.26 declares constant-specific class bodies for its constants. The following skeletal code declares the constant-specific class body for the enum constant…