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

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

Moderator: admin

Post Reply
ewebxml
Posts: 77
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

kilo1981
Posts: 3
Joined: Wed Apr 30, 2014 3:12 pm
Contact:

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

Post 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!

kilo1981
Posts: 3
Joined: Wed Apr 30, 2014 3:12 pm
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

giorgiadiro
Posts: 7
Joined: Wed Feb 04, 2015 1:06 pm
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

eddie3
Posts: 14
Joined: Sat Dec 17, 2016 10:17 pm
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

eddie3
Posts: 14
Joined: Sat Dec 17, 2016 10:17 pm
Contact:

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

Post 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?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

eddie3
Posts: 14
Joined: Sat Dec 17, 2016 10:17 pm
Contact:

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

Post by eddie3 »

Oh, that makes perfect sense now. Thank you!

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

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

Post 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?

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

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

Post by admin »

You can cast null to any type. So even if there is no object, the JVM has no problem with the assignment.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests