Author: Bryan Cory
-
Polymorphism – Object-Oriented Programming
5.12 Polymorphism A supertype reference can denote an object of its subtypes—conversely, a subtype object can act as an object of its supertypes. This is because there is an is-a relationship between a subtype and its supertypes. Since a supertype reference can denote objects of different types at different times during execution, a supertype reference…
-
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…
-
Enum Values in Exhaustive switch Expressions – Object-Oriented Programming
Enum Values in Exhaustive switch Expressions A switch expression is always exhaustive—that is, the cases defined in the switch expression must cover all values of the selector expression type or the code will not compile (§4.2, p. 160). The switch expression below at (1) is exhaustive, as all values of the enum type Day are…
-
Record Classes – Object-Oriented Programming
5.14 Record Classes Exchanging plain data between programs is a common task. Plain data objects allow efficient aggregation of data and provide transparent access to it. In the literature, such data objects are referred to by different names; the acronym POJOs (Plain Old Java Objects) is common in the Java community. A plain data class…
-
Record Class Basics – Object-Oriented Programming
Record Class Basics A record class in Java is a special-purpose class that simplifies declaration and handling of an aggregate of values that comprise the state of a plain data object. A record class defines immutable fields and the compiler generates the get methods (also called getter or accessor methods) necessary to access the values…
-
Record Class Basics 2 – Object-Oriented Programming
The compiler automatically generates the necessary declarations for a record class when it is declared according to the simplified syntax form, in particular, when the record body is empty. Fields being final means that once a final field is initialized, its value cannot be changed. However, if a field refers to a mutable object, this…
-
Enum Types – Object-Oriented Programming
5.13 Enum Types An enum type is a special-purpose class that defines a finite set of symbolic names and their values. These symbolic names are usually called enum constants or named constants. An enum type is also synonymously referred to as an enum class. Before the introduction of enum types in the Java programming language,…
-
Augmenting Basic Record Class Declaration – Object-Oriented Programming
Augmenting Basic Record Class Declaration In the rest of this section, we explore how the basic declaration of a record class can be augmented and customized. The general syntax of a record class is shown below. Click here to view code image access_modifier recordrecord_name(component_list)optional_implements_clause {optional_constructor_declarationsoptional_member_declarations} The basic declaration of a record class can be augmented with an…
-
Using Type-Safe Enums – Object-Oriented Programming
Using Type-Safe Enums Example 5.24 illustrates using enum constants. An enum type is essentially used as any other reference type, and the restrictions are noted later in this section. Enum constants are actually final, static variables of the enum type, and they are implicitly initialized with instances of the enum type when the enum type…