Category: Sealed Classes and Interfaces
-
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…
-
Sealed Classes and Interfaces – Object-Oriented Programming
5.15 Sealed Classes and Interfaces Design by inheritance promotes code reuse in the OOP model—where subtypes can inherit code from their supertypes. Such an inheritance hierarchy is depicted in Figure 5.8, where the subclasses PrintedBook, Ebook, and Audiobook can inherit and reuse code from the superclass Book. The inheritance hierarchy of the Book superclass can…
-
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…
-
Arrays and Subtyping – Object-Oriented Programming
5.7 Arrays and Subtyping Table 5.4 summarizes the types found in Java. Only primitive data and reference values can be stored in variables. Only class and array types can be explicitly instantiated to create objects. Table 5.4 Types and Values Types Values Primitive data types Primitive data values Class, interface, enum, and array types (reference…
-
The instanceof Type Comparison Operator – Object-Oriented Programming
The instanceof Type Comparison Operator The binary instanceof operator can be used for comparing types. It has the following syntax when used as a type comparison operator (note that the keyword is composed of lowercase letters only): Click here to view code image reference_expression instanceofdestination_type The instanceof type comparison operator returns true if the left-hand operand…
-
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…
-
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…
-
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…
-
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,…
-
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…