Question: What will be the result of compiling and running the following program? Select the one correct answer. (a) The program will fail to compile. (b)

What will be the result of compiling and running the following program?

public class Polymorphism ( public static void main (String[] args) { A

Select the one correct answer.

(a) The program will fail to compile.

(b) The program will compile, but will throw a ClassCastException at runtime.

(c) The program will compile, and print 0 when run.

(d) The program will compile, and print 1 when run.

(e) The program will compile, and print 2 when run.

public class Polymorphism ( public static void main (String[] args) { A refl= new C(); B ref2 = (B) refl; System.out.println (ref2.f()); } } class A class B extends A { int f() { class C extends B { int f() { { int f() { return 0; } } return 1; } } return 2; } }

Step by Step Solution

3.30 Rating (144 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

e The program will print 2 when Systemoutprintlnref2f is executed The o... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Java Programming 8th Questions!