What will be the result of attempting to compile and run the following program?
public class Polymorphism { public static void main(String[] args) { A ref1 = new C(); B ref2 = (B) ref1; System.out.println(ref2.f()); } } class A { int f() { return 0; } } class B extends A { int f() { return 1; } } class C extends B { int f() { return 2; } }
Select the one correct answer.
The program will fail to compile.
The program will compile without error, but will throw a ClassCastException when run.
The program will compile without error and print when run.
The program will compile without error and print 1 when run.
The program will compile without error and print 2 when run.
6.26
What will be the result of attempting to compile and run the following program?
public class Polymorphism2 { public static void main(String[] args) { A ref1 = new C(); B ref2 = (B) ref1; System.out.println(ref2.g()); } } class A { private int f() { return 0; } public int g() { return 3; } } class B extends A { private int f() { return 1; } public int g() { return f(); } } class C extends B { public int f() { return 2; } }
Select the one correct answer.
The program will fail to compile.
The program will compile without error and print when run.
The program will compile without error and print 1 when run.
The program will compile without error and print 2 when run.
The program will compile without error and print 3 when run.