About Question com.enthuware.ets.scjp.v6.2.772 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question com.enthuware.ets.scjp.v6.2.772 :

Post by ETS User »

I wrote code below into Eclipse (copy & paste from question window) and it runs fine. Your proper answer is 1, this is obvious. But what about answer 2? You said it is wrong, and it seems like it, yet my test tell something different (no Eclipse error, no build error, no exception during runtime).

Thanks for explanation.
MM

Code: Select all

public class InheritanceTest {
	public static void main(String[] args) {
		A a = new A();
		B b = new B();

		// 1. works fine
		a = (B) (I) b;

		// 2. works fine - why?
		b = (B) (I) a;

		// 3. won't compile
		// a = (I) b;

		// 4. exception during runtime
		// I i = (C) a;
	}
}

interface I {}
class A implements I {}
class B extends A {}
class C extends B {}

Guest

Re: About Question com.enthuware.ets.scjp.v6.2.772 :

Post by Guest »

After a while I found an answer...

Line 2 works fine only because of line 1 is executed before (and it changes a reference). If line 1 is commented it throws exception during runtime. I shouldn't put all four answers in the same method.

Post Reply

Who is online

Users browsing this forum: No registered users and 231 guests