Blog

Hiding Static Methods Only instance methods in an object can be overridden. However, a static method in a subclass can hide a static method from the superclass. Hiding a static method is analogous to overriding an instance method except for one important aspect: Calls to static methods are bound at compile time as opposed to runtime for calls to overridden instance methods, and therefore do not exhibit polymorphic behavior exhibited by calls to overridden instance methods (p. 278). When hiding a static method, the compiler will flag an error if the signatures are the same, but the other requirements regarding the return type, throws clause, and accessibility are not met. If the signatures are different, the method name is overloaded, not hidden. A static method in the subclass can only hide a static method in the superclass. Analogous to an overridden instance method, a hidden superclass static method is not…

Read more

4.9 Transfer Statements Java provides the following language constructs for transferring control in a program: The throw statement can also transfer control in a program (§7.4, p. 386). 4.10 Labeled Statements A statement may have a label: label :statement A label is any valid identifier; it always immediately precedes the statement. Label names exist in their own namespace, so that they do not conflict with names of packages, classes, interfaces, methods, fields, and local variables. The scope of a label is the statement prefixed by the label, meaning that it cannot be redeclared as a label inside the labeled statement—analogous to the scope of local variables. Click here to view code image L1: if (i > 0) {  L1: System.out.println(i);    // (1) Not OK. Label L1 redeclared.}L1: while (i < 0) {             // (2) OK.  L2: System.out.println(i);}L1: {                           // (3) OK. Labeled block.  int j = 10;  System.out.println(j);}L1: try {                       //…

Read more

4.12 The continue Statement Like the break statement, the continue statement comes in two forms: unlabeled and labeled. Click here to view code image continue;             // the unlabeled formcontinuelabel;        // the labeled form The continue statement can be used only in a for(;;), for(:), while, or do-while loop to prematurely stop the current iteration of the loop body and proceed with the next iteration, if possible. In the case of the while and do-while loops, the rest of the loop body is skipped—that is, the current iteration is stopped, with execution continuing with the loop condition. In the case of the for(;;) loop, the rest of the loop body is skipped, with execution continuing with the update expression. In Example 4.12, an unlabeled continue statement is used to skip an iteration in a for(;;) loop. Control is transferred to (2) when the value of i is equal to 4 at…

Read more

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…

Read more

5.1 Implementing Inheritance Inheritance is one of the fundamental mechanisms for code reuse in OOP. It allows new classes to be derived from existing ones. The new class (also called a subclass, subtype, derived class, or child class) can inherit members from the old class (also called a superclass, supertype, base class, or parent class). The subclass can add new behavior and properties and, under certain circumstances, modify its inherited behavior. A subclass specifies the name of its superclass in the subclass header using the extends clause. Click here to view code image class TubeLight extends Light { … }   // TubeLight is a subclass of Light. The subclass specifies only the additional new and modified members in its class body. The rest of its declaration is made up of its inherited members. If no extends clause is specified in the header of a class declaration, the class implicitly inherits…

Read more

The Subtype–Supertype Relationship A class defines a reference type, a data type whose objects can be accessed only by references. Therefore, the inheritance hierarchy can be regarded as a type hierarchy, embodying the subtype–supertype relationship between reference types. The subclass– superclass relationship is a special case of the subtype–supertype relationship that is between classes. The subclass–superclass relationship allows single inheritance of type, meaning that the subclass inherits the type of its direct superclass. This is in contrast to a class that implements several interfaces, resulting in multiple inheritance of type—that is, a class inherits the type of all interfaces it implements. In the context of Java, the subtype–supertype relationship implies that the reference value of a subtype object can be assigned to a supertype reference because a subtype object can be substituted for a supertype object. This assignment involves a widening reference conversion, as references are assigned up the inheritance…

Read more

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…

Read more

Covariant return in Overriding Methods In Example 5.2, the definition of the method makeInstance() at (9) overrides the method definition at (3). Click here to view code image // The overridden method in the superclass Light:public Light makeInstance() { … }     // (3) Instance method // The overriding method in the subclass TubeLight:@Overridepublic TubeLight makeInstance() { … } // (9) Overriding instance method at (3). Note that the method signatures are the same, but the return type at (9) is a subtype of the return type at (3). The method at (9) returns an object of the subtype Tube-Light, whereas the method at (3) returns an object of the supertype Light. This is an example of covariant return. Depending on whether we call the method makeInstance() on an object of the subtype TubeLight or an object of the supertype Light, the respective method definition will be executed. The code at…

Read more

4.13 The return Statement The return statement is used to stop execution of a method (or a constructor) and transfer control back to the calling code (also called the caller or invoker). The usage of the two forms of the return statement is dictated by whether that statement is used in a void or a non-void method (Table 4.2). The first form does not return any value to the calling code, but the second form does. Note that the keyword void does not represent any type. In Table 4.2, the expression must evaluate to a primitive value or a reference value, and its type must be assignable to the return type specified in the method header (§2.7, p. 54, and §5.9, p. 261). See also the discussion on covariant return in connection with method overriding in §5.1, p. 201. As can be seen from Table 4.2, a void method need…

Read more

Hiding Fields A subclass cannot override inherited fields of the superclass, but it can hide them. The subclass can define fields with the same name as in the superclass. If this is the case, the fields in the superclass cannot be accessed in the subclass by their simple names; therefore, they are not inherited by the subclass. A hidden static field can always be accessed by using the superclass name in the subclass declaration. Additionally, the keyword super can be used in non-static code in the subclass declaration to access hidden static fields. The following distinction between invoking instance methods on an object and accessing fields of an object must be noted. When an instance method is invoked on an object using a reference, it is the dynamic type of the reference (i.e., the type of the current object denoted by the reference at runtime), not the declared type of…

Read more

30/30