Suppose that the following variables referring to the classes from the previous
ID: 3848711 • Letter: S
Question
Suppose that the following variables referring to the classes from the previous problem are declared:
Pond var1 = new Bay();
Object var2 = new Ocean();
Which of the following statements produce compiler errors? For the statements that do not produce errors, what is the output of each statement?
((Lake) var1).method1();
((Bay) var1).method1();
((Pond) var2).method2();
((Lake) var2).method2();
((Ocean) var2).method3();
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
COULD SOMEBODY ALSO ADD THE POND AND OBJECT VARIABLES TO MY CLASS CODES? MY BIGGEST PROBLEM IS NOT KNOWING WHERE TO ADD THEM IN
<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
public class Bay extends Lake{
public void method1() {
System.out.print("Bay 1 ");
super.method2();
}
public void method2() {
System.out.print("Bay 2 ");
}
}
public class Pond {
public void method1() {
System.out.print("Pond 1 ");
}
public void method2() {
System.out.println("Pond 2 ");
}
public void method3() {
System.out.println("Pond 3 ");
}
}
public class Ocean extends Bay {
public void method2() {
System.out.print("Ocean 2 ");
}
}
public class Lake extends Pond {
public void method3() {
System.out.print("Lake 3 ");
method2();
}
}
<><><><>MAIN CLASS<><><><>
public class BayMain {
public static void main(String[] args) {
Pond[] ponds = {new Ocean(), new Pond(), new Lake(), new Bay()};
for (Pond p : ponds) {
p.method1();
System.out.println();
p.method2();
System.out.println();
p.method3();
System.out.println(" ");
}
}
}
Explanation / Answer
Hi,
Please find the answer for the above question.
public class Bay extends Lake{
public void method1() {
System.out.print("Bay 1 ");
super.method2();
}
public void method2() {
System.out.print("Bay 2 ");
}
}
public class Pond {
public void method1() {
System.out.print("Pond 1 ");
}
public void method2() {
System.out.println("Pond 2 ");
}
public void method3() {
System.out.println("Pond 3 ");
}
}
public class Ocean extends Bay {
public void method2() {
System.out.print("Ocean 2 ");
}
}
public class Lake extends Pond {
public void method3() {
System.out.print("Lake 3 ");
method2();
}
}
public class BayMain {
public static void main(String[] args) {
Pond[] ponds = {new Ocean(), new Pond(), new Lake(), new Bay()};
for (Pond p : ponds) {
p.method1();
//Output and explanation:
//i) Bay 1 Pond 2 (Ocean object came ,but method1 is available in Bay so calling method1 of Bay and in method1 of Bay calling super class method2 of Pond)
//ii) Pond 1
//iii)Pond 1
//iV) Bay 1 Pond 2
System.out.println();
p.method2();
//Output:
//Ocean 2 (Ocean object came method2 is calling, available in Ocean)
//ii) Pond 2
//iii)Pond 2
//iv) Bay 2
System.out.println();
p.method3();
//Output: Lake 3 Ocean 2 (Ocean object came method3 is calling, but available in lake )
//ii) Pond 3
//iii)Lake 3 Pond 2
//iv) Lake 3 Bay 2
System.out.println(" ");
}
Pond var1 = new Bay();
Object var2 = new Ocean();
((Lake) var1).method1(); //Output: Bay 1 Pond 2
((Bay) var1).method1(); //Output: Bay 1 Pond 2
((Pond) var2).method2(); //Output: Ocean 2
((Lake) var2).method2(); //Output: Ocean 2
((Ocean) var2).method3(); //Output: Lake 3 Ocean 2
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.