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

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

Moderator: admin

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

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

Post by admin »

Since C is a subclass of A, the compiler knows that any object of class C will always satisfy the is-a test for A. For example, a Dog is always an Animal. So you can assign a Dog object to a variable of type Animal without any cast. The compiler has no problem there.

Now, if you have a variable of type Animal, you know that this variable can point to a Dog or a Cat or any other animal. The compiler doesn't execute any code so it does not really know for sure which exact object is this variable will be referring to at run time. So when you assign a variable of type Animal to a variable of type Dog, ( or o1 to o2, in this case), you need to assure the compiler that at run time my variable will indeed point to a Dog object. You assure the compiler through an explicit cast.

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

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

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

Post by mjmsausava »

admin wrote: So when you assign a variable of type Animal to a variable of type Dog, ( or o1 to o2, in this case), you need to assure the compiler that at run time my variable will indeed point to a Dog object. You assure the compiler through an explicit cast.
That cleared my confusion. I was (wrongly) thinking that "B o2 = (B) o1" was pointing to the object of C, created in the previous line. Thanks Paul :)

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

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

Post by admin »

mjmsausava wrote:
admin wrote: So when you assign a variable of type Animal to a variable of type Dog, ( or o1 to o2, in this case), you need to assure the compiler that at run time my variable will indeed point to a Dog object. You assure the compiler through an explicit cast.
That cleared my confusion. I was (wrongly) thinking that "B o2 = (B) o1" was pointing to the object of C, created in the previous line. Thanks Paul :)
It is! But the compiler doesn't know that.
If you like our products and services, please help us by posting your review here.

Arold Aroldson
Posts: 21
Joined: Mon Nov 20, 2017 8:00 am
Contact:

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

Post by Arold Aroldson »

Hi!

Why B o2 = (B) o1; doesn't throw ClassCastException?

A o1=new C(); //ok. As C is-an A. Now, actual object that o1 is refered to is C.
B o2=(B) o1 //Here we say compiler that the object o1 refered to is B. But it's not.

I don't get how it works...

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

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

Post by admin »

Yes, o1 is points to an object of class C. But C extends B, right? It is like saying o1 is pointing to an object of class Apple, but since Apple is a Fruit, it is ok to assign it to a reference of type Fruit. That is why B o2 = (B) o1; is valid. Also, the cast is not required but it is not wrong to have it.

If you are having trouble understanding this, you might want to go through Section 11.3 of OCAJP Associate Java 8 Programmer Certification Fundamentals by Hanumant Deshmukh. You can download it for free from here for the time being.

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

Arold Aroldson
Posts: 21
Joined: Mon Nov 20, 2017 8:00 am
Contact:

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

Post by Arold Aroldson »

Why casting is not required? B o2 = o1 won't compile.
Ok i will read that book because i thought that you won't get casting exception only if the object you are trying to cast is the same class as class within brackets and i would get the exception even if the class within brackets is it's superclass i.e.:

(C) o1 // OK
(B) o1 //casting exception
(A) o1 //casting exception
(Object) o1 //casting exception

Seems like i missed something important...

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

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

Post by admin »

Arold Aroldson wrote:
Wed Sep 26, 2018 5:38 am
Why casting is not required? B o2 = o1 won't compile.
Sorry, my mistake. The fact that the type of o1 is A, slipped from my mind while I was writing that. So yes, you are right. That cast is required.
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 55 guests