About Question enthuware.ocajp.i.v7.2.993 :

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

Moderator: admin

Post Reply
aida.alemu
Posts: 3
Joined: Mon Mar 31, 2014 10:37 am
Contact:

About Question enthuware.ocajp.i.v7.2.993 :

Post by aida.alemu »

// Filename: A.java
class A{
public static void main(String args[]){
A a = new A();
B b = new B();
a = b; // 1
b = a; // 2
a = (B) b; // 3
b = (B) a; // 4
}
}
class B extends A { }

in this case, I am not sure why line 2 is wrong. Doesn't line one assign the values of b to a. Therefore, doesn't a end up representing the same instance that b is referring to; which I thought would make line 2 just a(holding value of b) being assigned to b (basically representing the same thing).
Just a little confused about this. Thank You.

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

Re: About Question enthuware.ocajp.i.v7.2.993 :

Post by admin »

You need to differentiate between information available with the compiler at compile time and the information available with the JVM at runtime.
Further, knowing a fact and making use of that fact to draw inferences are two different things. In some cases, the compiler may know a fact but it may not be able to use that fact to draw an inference.

The general idea here is that a compiler only makes use of the facts about declared type of references and not about the actual types of the objects to which the references are pointing. The JVM makes use of the actual object types at runtime.

Therefore,
Even though the compiler knows the fact that at //1 a is being assigned an object of class B, the compiler cannot make use of that fact to allow b = a; at //2 because the declared type of a is class A, which is all that the compiler can use. It cannot use the fact about the actual object to which a is referring.

This is how the language has been designed. There is no other reason.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.993 :

Post by Javier »

Hi Paul!
I would like to know why:
b=(B)a;// (I made the casting.)
is not throwing ClassCastException? if a is not an instance of B (It is exactly the opposite).
Thank you very much Paul,
( I am upset because I thought that I understood this :( )

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

Re: About Question enthuware.ocajp.i.v7.2.993 :

Post by admin »

There are two lines of code marked //1 and //2 before your cast -
a = b; <-- This makes a point to the object b is pointing to. b is pointing to an instance of B.
b = a; <-- This makes b point to the object a is pointing. a was made to point to an instance of B in the previous line.

So, why do you think a is not an instance of B?

If you are in doubt, you can always print the actual class of object pointed to by any variable like this:
System.out.println(a.getClass());
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.993 :

Post by flex567 »

In case we are assigning superclass type variable to subclass type variable downcasting(with brackets) is always needed?

Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests