About Question enthuware.ocajp.i.v7.2.1002 :
Moderator: admin
-
- Posts: 77
- Joined: Sun Jun 30, 2013 10:04 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1002 :
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?
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?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
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.
HTH,
Paul.
-
- Posts: 3
- Joined: Wed Apr 30, 2014 3:12 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
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!
I2 i2 = (I2) i1;
Why does i1 at runtime refer to an object that implements the interface I2? I'm really confused!

-
- Posts: 3
- Joined: Wed Apr 30, 2014 3:12 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
OK, is it that we need to take both part 1 and part 2 of Option 4 together?
The first part:
assigns o3 to i1
then in the second part we are essentially saying
Which is OK because C3 implements I2?
The first part:
Code: Select all
I1 i1 = o3;
then in the second part we are essentially saying
Code: Select all
I2 i2 = (I2) o3;
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
Yes, both the statements are part of the same option and must be considered together.kilo1981 wrote:OK, is it that we need to take both part 1 and part 2 of Option 4 together?
HTH,
Paul.
-
- Posts: 7
- Joined: Wed Feb 04, 2015 1:06 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
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?
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?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
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.
-
- Posts: 14
- Joined: Sat Dec 17, 2016 10:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
Regarding to the post above, I tried this in Java:
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?
Code: Select all
class U1 {}
class U2{
public static void main (String[] arguments) {
U1 a = new U1();
U2 b = (U2) a; //Won't compile
}
}
So does that mean that the compiler do care about "is-a" relationship between to object and object casted?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
You seem to have ignored the second sentence of the post that you mentioned:
Paul.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.
-
- Posts: 14
- Joined: Sat Dec 17, 2016 10:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
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?
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
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.
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.
-
- Posts: 14
- Joined: Sat Dec 17, 2016 10:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
Oh, that makes perfect sense now. Thank you!
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
The explanation says:
but there are no objects, only references?This is valid because at run time i1 actually refers to an object that implements I2.
-
- Site Admin
- Posts: 10386
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1002 :
You can cast null to any type. So even if there is no object, the JVM has no problem with the assignment.
Who is online
Users browsing this forum: Bing [Bot] and 9 guests