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.

jerry___
Posts: 9
Joined: Tue Nov 26, 2024 2:08 pm
Contact:

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

Post by jerry___ »

I'm struggling to see why this option does not compile.
a = (I) b;

This complies to everything in my mental checklist for casting:
  • b is-an I so (I) b is OK. This part by itself could even be done implicitly since it is a broadening conversion.
  • Casting changes the reference type so I mentally replace this with: I a = (I)b; (or, since "a" is already defined within scope, a new reference "a1")
So in my mind, the original reference type (A) would not be relevant and the only factors in play would be the object type (B) and the target reference type (I). The answer, explanation and compiler all seem to disagree with me so I'm trying to make sense of it.

How does the original reference type fit into this set of rules?

admin
Site Admin
Posts: 10382
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

You are missing the point that a is declared to be of type A and not of type I.

1. You can imagine A to contain the stuff provided by I and some extra stuff besides I.
It is like Car implements Movable but Car also contains lots of other stuff besides being a Movable.

2. So, a is supposed to point to an object that can provide all that A promises to provide. Which means, a can only point to an object of type A or a subclass of A.

3. (I) b; means that now you have a new reference (let's call it b2) which points to an object of type I. But an object that implements I may not necessarily be of type A. It could point to an entirely different type of object like StockPrice, which is Movable but is not a Car.

4. Therefore, you cannot assign the b2 to a in the same manner as you cannot assign a StockPrice object to a Car variable.

jerry___
Posts: 9
Joined: Tue Nov 26, 2024 2:08 pm
Contact:

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

Post by jerry___ »

Thank you for this explanation.

My shortcut mnemonic "Casting changes the reference type" should be more nuanced:

After performing a succesful casting operation, the result is similar to changing the reference type.
The casting operation is only succesful if an instance of the new reference type is assignable to the original reference type.

Post Reply

Who is online

Users browsing this forum: No registered users and 95 guests