Question: Experiment 3: Type the following code into the main method of Experiment_3 and try to run the main method. You may have to comment out

 Experiment 3: Type the following code into the main method of
Experiment_3 and try to run the main method. You may have to
"comment out" statements as you test to see which are valid and

Experiment 3: Type the following code into the main method of Experiment_3 and try to run the main method. You may have to "comment out" statements as you test to see which are valid and which are not. What happens if we try to call a Rectangle's method using a reference if type Shape? Shape S; Rectangle r = new Rectangle ( 10, 20); System.out.println( r.getLength()); S; System.out.println( s.getLength() ); Experiment 3 further demonstrates need to understand the distinction between the variable holding the reference and the object to which it refers In the code segment Shape s: Rectangle r = new Rectangle ( 10, 20); S-r; Now variable s holds a reference to an object of type Shape, that's precisely what the declaration "Shape s;" says. So, Java will be aware of the presence of all methods and instance variables specifically defined within the Shape class (or its superclasses). Now (this is important) we are permitted to put a reference to a Rectangle object into a Shape variable because inheritance indicates a Rectangle is a Shape. So in the code segment above, the reference type is a Shape but the actual referenced object is a Rectangle. If we use the reference variable to call any methods or make any instance variable reference, Java will only be aware of those things listed in the Shape class EVEN THOUGH the actually object, a Rectangle, has additional information Here we will be told that the method getLength() cannot be found as it does not exist in the Shape class. This error occurs because, as far as Java knows, s refers to a Shape not a Rectangle specifically. Since it could refer to any of several shapes, it is unable to resolve the call to the method. We actually can make this work using a cast. If we first cast the Shape reference to a Rectangle then the call will work as desired System.out.println( ((Rectangie)s) got Length()); Now, there is only one reason why one would want to make a cast - to use an object in its full capacity after its actual type has been temporarily forgotten (i.c. the reference the actunl Rectangle object is contained in a Shape variable). Java checks that you do not promise too much when you store a value in a variable. For example, int X-3.14; // assumes int could contain digits in 3.14 (You are promising too much) IMPORTANT In terms of classes, a) If you assign a subclass reference to a superclass variable, you are promising less, and Java will comply and let you do it b) If you assign a superclass reference to a subclass variable, you are promising more. In this case, you MUST use a cast so that your promise can be checked at RUN-TIME. For example, in our example, Shape is the superclass and Rectangle and circle are subclasses of Shape, Shape : Rectangle new Rectangle( 10, 20); KEEP OR NOT?? Circled - new Circle 5, "Red ) SC V/ WORKS! Subclass reference to superclass variable (a Rectangle is a Shape) / WORKS! Subclass reference to superclass variable a circle is a Shape) W/ FAILS! Superclass reference to subclass variable (promising too much!) An error will be detected at COMPILE TIME! ES - (Rectangle) 57 // Legal but if a does not refer to Rectangle (1.6. it could refer to a circle), an Tror will be detected At RUNTIME: - INITIAL TEMPLATE Experiment_3 x Compile Undo Cut Copy Paste Find... Close Source Code /** * Class Experiment_3 */ import java.util.*; public class Experiment_3 { public static void main(String[] args) { }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!