Question: Study the Java code below: public interface I { void f(); } public class A implements I { public void f() {System.out.println(A.f);} } public class
Study the Java code below:
public interface I {
void f();
}
public class A implements I {
public void f() {System.out.println("A.f");}
}
public class B extends A {
public void g() {System.out.println("B.g");}
}
public class C extends B {
public void g() {System.out.println("C.g");}
}
public class Tester {
I varI;
public static void main(String[] args) {
A varA = new A();
B varB = new B();
C varC = new C();
//new code goes here
}
}
What happens when the following code is inserted (each question is independent). Assume we then try to compile and run the program. If
there is a compiler error say Compiler Error and why.
If there is a runtime exception, say which one. If there is no error or exception, say
No error and what the output is, if there is any. The first one is done as an example.
You should run the code to see what happens.
a. varA = varC;
___________________________________________________________________
b.
varI = varB;
___________________________________________________________________
c.
I i = new I();
___________________________________________________________________
d.
varB = (A) varC;
___________________________________________________________________
e.
varA.g();
__________________________________________________________________
f.
((B) varA).g();
__________________________________________________________________
g.
((B) varC).f();
__________________________________________________________________
h.
varA = varB; varA.g();
_________________________________________________________________
i.
varA = varB; ((C) varA).g();
_____________________________________________________________
j.
varA = varC; ((B) varA).g();
_________________________________________________________
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
