What is the result?

Given:

class Alpha {
public void foo() { System.out.print(“Afoo “); }
}
public class Beta extends Alpha {
public void foo() { System.out.print(“Bfoo “); }
public static void main(String[] args) {
Alpha a = new Beta();
Beta b = (Beta)a;
a.foo();
b.foo();
}
}

What is the result?

Given:

class Alpha {
public void foo() { System.out.print(“Afoo “); }
}
public class Beta extends Alpha {
public void foo() { System.out.print(“Bfoo “); }
public static void main(String[] args) {
Alpha a = new Beta();
Beta b = (Beta)a;
a.foo();
b.foo();
}
}

What is the result?

A.
Afoo Afoo

B.
Afoo Bfoo

C.
Bfoo Afoo

D.
Bfoo Bfoo

E.
Compilation fails.

F.
An exception is thrown at runtime.



Leave a Reply 2

Your email address will not be published. Required fields are marked *