Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming language is JAVA: interface I { void f(); } public class A implement

ID: 3833684 • Letter: P

Question

Programming language is JAVA:

interface I { void f(); }

public class A implements I {

public void f() {

  System.out.println("A");}

}

public class B extends A {

public int n;

public void g() {

System.out.println("B");}

}

public class C extends B {

public void g() {

System.out.println("C");}

}

public class Tester {

static I varI;

public static void main(String[] a)

{

I varJ;

   A varA = new A();

   B varB = new B();

   C varC = new C();

   // new code goes here

}

}

Consider various statements to go at the end of main. Maybe the statement(s) involves an assignment, a method call, a class cast. On the e_x_a_m you'll be asked what outcome is produced when we add the statement(s). For example, if we add the statement varB = varA; there would be a compiler error because the assignment is in the wrong direction. Or if we have the statement varB = varC; followed by varB.g() what would the output be?

Explanation / Answer

I have added some code with explanation.

public class Tester {

static I varI;

public static void main(String[] a)

{

   I varJ;

   A varA = new A();

   B varB = new B();

   C varC = new C();

   // new code goes here

   varA = varB; // it is fine, Parent class can hold the object of child class

//1. varA.ge(); // we can not call g() method using reference of A, because it is not in A,

   // althouh varA hold the object of A

   ((B)varA).g(); // you can call like this

   varB = varC;// it is fine, Parent class can hold the object of child class

   varC.f();

   varC.g(); // this will all the C's method, not B's method

   // you can not call B's g() method using C's reference

}

}