Page 1 of 1

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

Posted: Mon Nov 04, 2013 3:26 am
by ewebxml
Given the following definitions and reference declarations:

interface I1 { }
interface I2 { }
class C1 implements I1 { }
class C2 implements I2 { }
class C3 extends C1 implements I2 { }
C1 o1;
C2 o2;
C3 o3;
Which of these statements are legal?


For the question above

Option a. has the following listed.
class C4 extends C3 implements I1, I2 { }

Question: Where did the class C4 come from?
Is this a mistake?

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

Posted: Mon Nov 04, 2013 6:39 am
by admin
No, it is a new class that this option is trying to declare. C1, C2, C3 already exist as given in the question.
HTH,
Paul.

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

Posted: Sat May 03, 2014 6:20 pm
by kilo1981
Can you explain more about option 4, part 2:
I2 i2 = (I2) i1;

Why does i1 at runtime refer to an object that implements the interface I2? I'm really confused! :? Thanks!

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

Posted: Sat May 03, 2014 6:23 pm
by kilo1981
OK, is it that we need to take both part 1 and part 2 of Option 4 together?

The first part:

Code: Select all

I1 i1 = o3; 
assigns o3 to i1

then in the second part we are essentially saying

Code: Select all

I2 i2 = (I2) o3;
Which is OK because C3 implements I2?

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

Posted: Sat May 03, 2014 8:24 pm
by admin
kilo1981 wrote:OK, is it that we need to take both part 1 and part 2 of Option 4 together?
Yes, both the statements are part of the same option and must be considered together.

HTH,
Paul.

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

Posted: Mon Apr 06, 2015 5:40 am
by giorgiadiro
the second part of option 4: i2 = (I2) i1;

I understand that at runtime it's ok: i1 refers to an object C3 that implements I2.
But at compile time i1 is a I1. How can we cast I1 to I2? They haven't a is-a relation.
The compiler knows that are existing some class that implements I1 and I2 and it understand that this could be a valid cast?

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

Posted: Mon Apr 06, 2015 10:55 am
by admin
Type of the variable doesn't have to have a is-a relation for the cast to succeed at compile time. The compiler only cares if there is a possibility of is-a relationship between the actual object that is being casted to the type to which it is casted.

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

Posted: Mon Dec 19, 2016 3:32 pm
by eddie3
Regarding to the post above, I tried this in Java:

Code: Select all

class U1 {}
class U2{
    public static void main (String[] arguments) {
        U1 a = new U1();
        U2 b = (U2) a;   //Won't compile
    }
}
And the compiler gives me an error, saying that "incompatible types: U1 cannot be converted to U2".
So does that mean that the compiler do care about "is-a" relationship between to object and object casted?

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

Posted: Mon Dec 19, 2016 8:26 pm
by admin
You seem to have ignored the second sentence of the post that you mentioned:
The compiler only cares if there is a possibility of is-a relationship between the actual object that is being casted to the type to which it is casted.
Paul.

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

Posted: Tue Dec 20, 2016 12:39 am
by eddie3
But in the case of this question, how can there be any possibility that I1 and I2 have a is-a relationship since there's no hierarchy between the two interfaces?

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

Posted: Tue Dec 20, 2016 1:05 am
by admin
I1 and I2 don't necessarily need to have any is-a relationship. You can have a class that extends C3 and implements I1 as well, right? The compiler can see this possibility and accepts the code.
In case of your example, the type of a is U1 and you are trying to assign it to U2. But there is no way the variable a could ever point to an object of a class that satisfies is-a relationship to U1. The compiler figures this out and rejects the cast.

If you had something like this:
I1 a = null;
U2 b = (U2) a;
This is ok even if U2 does not implement I1 because the compiler knows that it is possible to write a class that extends U2 and implements I1. Therefore, it is possible for a to point to an object of a class that is-a U2. This proves that there is no need for I1 and U2 to have any is-a relationship between themselves.

HTH,
Paul.

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

Posted: Tue Dec 20, 2016 3:19 am
by eddie3
Oh, that makes perfect sense now. Thank you!

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

Posted: Sat May 26, 2018 6:05 am
by flex567
The explanation says:
This is valid because at run time i1 actually refers to an object that implements I2.
but there are no objects, only references?

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

Posted: Mon May 28, 2018 9:18 pm
by admin
You can cast null to any type. So even if there is no object, the JVM has no problem with the assignment.