About Question enthuware.ocajp.i.v7.2.1273 :
Moderator: admin
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1273 :
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.
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.
-
- Posts: 19
- Joined: Sat Mar 25, 2017 5:38 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1273 :
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 Pauladmin 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.
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1273 :
It is! But the compiler doesn't know that.mjmsausava wrote: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 Pauladmin 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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Mon Nov 20, 2017 8:00 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1273 :
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...
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...
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1273 :
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 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.
-
- Posts: 21
- Joined: Mon Nov 20, 2017 8:00 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1273 :
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...
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...
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1273 :
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.Arold Aroldson wrote: ↑Wed Sep 26, 2018 5:38 amWhy casting is not required? B o2 = o1 won't compile.
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 100 guests