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

Moderator: admin

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

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

Post by admin » Mon Apr 17, 2017 10:11 pm

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 » Tue Apr 18, 2017 9:32 am

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: 7451
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Tue Apr 18, 2017 9:44 am

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: 17
Joined: Mon Nov 20, 2017 8:00 am
Contact:

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

Post by Arold Aroldson » Wed Sep 26, 2018 12:30 am

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: 7451
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Wed Sep 26, 2018 1:42 am

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: 17
Joined: Mon Nov 20, 2017 8:00 am
Contact:

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

Post by Arold Aroldson » Wed Sep 26, 2018 5:38 am

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: 7451
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Wed Sep 26, 2018 6:31 am

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: Bing [Bot] and 2 guests