In: Computer Science
What is the output produced by each statement below? If the statement causes an error, comment it out and write either "compiler error" or "runtime error" as appropriate. For each line make your guess and then move the open comment down a line to test your theory.
public class Main { public static void main(String[] args) { First var1 = new Second(); First var2 = new Third(); First var3 = new Fourth(); Second var4 = new Third(); Object var5 = new Fourth(); Object var6 = new Second(); /* var1.method2(); var2.method2(); var3.method2(); var4.method2(); //var5.method2(); //var6.method2(); var1.method3(); ((Third)var4).method1(); var2.method3(); var3.method3(); var4.method3(); var5.method3(); var6.method3(); ((Third)var4).method1(); ((Third)var4).method1(); ((Second)var5).method2(); ((First)var5).method3(); ((Third)var5).method1(); ((First)var6).method3(); ((Second)var6).method1(); ((Second)var6).method3(); ((Third)var6).method2(); */ } } class First extends Object{ public void method2() { System.out.println("First2"); } public void method3() { method2(); } } class Second extends First { public void method2() { System.out.println("Second2"); } } class Third extends Second { public void method1() { System.out.println("Third1"); super.method2(); } public void method2() { System.out.println("Third2"); } } class Fourth extends First { public void method1() { System.out.println("Fourth1"); } public void method2() { System.out.println("Fourth2"); } }
Required Solution-
Code version-
The corresponding outputs or errors are written as comment in front of each statement.
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main(String[] args) {
First var1 = new Second();
First var2 = new Third();
First var3 = new Fourth();
Second var4 = new Third();
Object var5 = new Fourth();
Object var6 = new Second();
var1.method2();// output- Second2
var2.method2();//output- Third2
var3.method2();//output - Fourth2
var4.method2();//output - Third2
//var5.method2();//compilation error, error: cannot find symbol
//var6.method2();//compilation error, error: cannot find symbol
var1.method3();//output - Second2
((Third)var4).method1();//output- Third1 Second2
var2.method3();// output- Third2
var3.method3();
var4.method3();//output - Fourth2
//var5.method3();//compilation error, error: cannot find symbol
//var6.method3();//compilation error, error: cannot find symbol
((Third)var4).method1();//output - Third2 Third1 Second2
((Third)var4).method1();//Output - Third1 Second2
//((Second)var5).method2();// Runtime error, java.lang.ClassCastException: Fourth cannot be cast to Second
((First)var5).method3();// output - Fourth2
//((Third)var5).method1();// Runtime error, java.lang.ClassCastException:: Fourth cannot be cast to Second
((First)var6).method3();// output - Second2
//((Second)var6).method1();//compilation error, error: cannot find symbol
((Second)var6).method3();//output - Second2
//((Third)var6).method2();//Runtime error, java.lang.ClassCastException: Second cannot be cast to Third
}
}
class First extends Object{
public void method2() {
System.out.println("First2");
}
public void method3() {
method2();
}
}
class Second extends First {
public void method2() {
System.out.println("Second2");
}
}
class Third extends Second {
public void method1() {
System.out.println("Third1");
super.method2();
}
public void method2() {
System.out.println("Third2");
}
}
class Fourth extends First {
public void method1() {
System.out.println("Fourth1");
}
public void method2() {
System.out.println("Fourth2");
}
}
Screenshot of solution