In: Computer Science
Consider the following classes:
public class Clock extends Bear { public void method3() { System.out.println("Clock 3"); } } public class Lamp extends Can { public void method1() { System.out.println("Lamp 1"); } public void method3() { System.out.println("Lamp 3"); } } public class Bear extends Can { public void method1() { System.out.println("Bear 1"); } public void method3() { System.out.println("Bear 3"); super.method3(); } } public class Can { public void method2() { System.out.println("Can 2"); method3(); } public void method3() { System.out.println("Can 3"); } }
---and that the following variables are defined:---
Object var1 = new Bear(); Can var2 = new Can(); Can var3 = new Lamp(); Bear var4 = new Clock(); Object var5 = new Can(); Can var6 = new Clock();
For each statement below , match the output produced in the right-hand column by the statement in the left-hand column. If the statement produces more than one line of output, the line breaks are indicated with slashes as in "a/b/c" to indicate three lines of output with "a" followed by "b" followed by "c". If the statement causes an error, choose in the right-hand column with either the phrase "compiler error" or "runtime error" to indicate when the error would be detected.
Note: Multiple statements can have the same output (ex: Compiler Error or Runtime Error)
var1.method2();
var2.method2();
var3.method2();
var4.method2();
var5.method2();
var1.method3();
var2.method3();
var3.method3();
var6.method3();
((Lamp)var6).method1();
((Can)var1).method1();
((Can)var1).method2();
((Bear)var1).method3();
((Clock)var1).method1();
---The choices for all methods are:---
Clock 3
Can 2/Can 3
Runtime Error
Compiler Error
Can 2/Bear 2/Can 3
Can 2/Clocl 3
Can 3
Bear3/Can 3
- Below is the code with the output.
- We need to add some cast to correct the compile time error
- There is a runtime error. I have added comments for the
same
- Runtime Exception at line 30 of below image due to wrong
reference as there is no method1 for clock class
- Compile time error at 3 places. added comments
Kindly upvote if
this helped
CODE:
- Created a main class for testing the results.
- Output is in above image
package search;
public class DummyTest {
public static void main(String[] args) {
Object var1 = new Bear();
Can var2 = new Can();
Can var3 = new Lamp();
Bear var4 = new Clock();
Object var5 = new Can();
Can var6 = new Clock();
((Can) var1).method2(); // compile time error - Add a cast here
var2.method2();
var3.method2();
var4.method2();
((Can) var5).method2(); // compile time error - Add a cast here
((Bear) var1).method3(); // compile time error - Add a cast here
var2.method3();
var3.method3();
var6.method3();
((Lamp) var6).method1(); // runtime error here due to wrong cast as method1 is in Lamp class . There is no method1 in clock class. as object is of class clock , so runtime error
((Bear) var1).method1(); // compile time error - change the cast here
((Can) var1).method2();
((Bear) var1).method3();
((Clock) var1).method1();
}
}
class Clock extends Bear {
public void method3() {
System.out.println("Clock 3");
}
}
class Lamp extends Can {
public void method1() {
System.out.println("Lamp 1");
}
public void method3() {
System.out.println("Lamp 3");
}
}
class Bear extends Can {
public void method1() {
System.out.println("Bear 1");
}
public void method3() {
System.out.println("Bear 3");
super.method3();
}
}
class Can {
public void method2() {
System.out.println("Can 2");
method3();
}
public void method3() {
System.out.println("Can 3");
}
}