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

Moderator: admin

Post Reply
bluster
Posts: 60
Joined: Wed Apr 23, 2014 6:38 pm
Contact:

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

Post by bluster » Fri Apr 25, 2014 3:40 pm

I am confused why the value of "i" declared in the object pointed to by the reference variable is not used.

Specifically, we do "A o1 = new C( );". Later we call o1's method, thusly:

System.out.println(o1.m1( ) );

Since "which variable will be used depends on the class that the variable is declared of", I should expect the version of the method m1 to be called from C (as it was), and the value of the variable i to be called from A, since the reference variable is of type A. Nevertheless, we use the value of i declared in C (30), not the value declared in A (10).

Please explain, if possible. Thanks!

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

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

Post by admin » Fri Apr 25, 2014 8:16 pm

Did you try running the code? System.out.println(o2.i ); does print i from B because the declared class of o2 is B as the explanation says.

If the declared class of o2 is B why would you expect i from A to be used?

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

bluster
Posts: 60
Joined: Wed Apr 23, 2014 6:38 pm
Contact:

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

Post by bluster » Sat Apr 26, 2014 8:31 am

Thanks for the reply, Paul. Sorry if not clear; my question was about o1, not o2. I agree that in o2 (as a B) there can be only one value of i, 20. It is in o1 that I do not understand why we we use the value of i declared in C (30), not the value declared in A (10). I know that it is so, and the compiler agrees; it is my skull that is in the way of understanding why.

Thanks again for the help.


**
note to self:Q=54, T=5

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

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

Post by admin » Sat Apr 26, 2014 9:12 am

Oh sorry. That is because we are not using o1 to access i. We are using o1 to access m1() and the method is picked up based on the actual class of object being referred to by the variable. Here, o1 refers to an object of class C so C's m1 is called. Now, within m1, you have

return i;

which is same as

return this.i;.

The declared class of 'this' is always same as the class in which it is used. Therefore, you are effectively using C's reference to access i, which returns 30.

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

bluster
Posts: 60
Joined: Wed Apr 23, 2014 6:38 pm
Contact:

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

Post by bluster » Sat Apr 26, 2014 10:48 am

Thanks, I think I got it now.

To confirm, if the code had been

System.out.println(o1.i ); ---> just the variable

We would get the "variable i from A" value, 10.

Since it ran "method m1 from C", we got the value of i as invoked by the method in C, which of course is C's value of i, 30.

Please confirm, if you can. Thanks again for the hand.

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

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

Post by admin » Sat Apr 26, 2014 12:46 pm

Yes, you got it :)
If you like our products and services, please help us by posting your review here.

bluster
Posts: 60
Joined: Wed Apr 23, 2014 6:38 pm
Contact:

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

Post by bluster » Sat Apr 26, 2014 1:19 pm

Great!

Thanks, Paul.

ElizabethCM
Posts: 29
Joined: Sun Apr 05, 2015 11:26 am
Contact:

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

Post by ElizabethCM » Thu May 28, 2015 2:13 pm

Hi,

I have some hard time understanding this:
Remember : variables are SHADOWED and methods are OVERRIDDEN.
Which variable will be used depends on the class that the variable is declared of.
Which method will be used depends on the actual class of the object that is referenced by the variable.


If we have these 2 lines:
(1) A o1 = new C( );
(2) B o2 = (B) o1;

According to what is written above:
Which is considered to be the class of the object?
Which is considered to be the class of the object referred by the variable?

in (1) => class of object is A, class of object referred by the variable is C???
in (2) =>
class of object is B
class of object referred by the variable is B

Please let me know if this is right. Thanks

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

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

Post by admin » Thu May 28, 2015 10:11 pm

ElizabethCM wrote:Hi,

If we have these 2 lines:
(1) A o1 = new C( );
(2) B o2 = (B) o1;

According to what is written above:
Which is considered to be the class of the object?
Which is considered to be the class of the object referred by the variable?

in (1) => class of object is A, class of object referred by the variable is C???
in (2) =>
class of object is B
class of object referred by the variable is B

Please let me know if this is right. Thanks
No, class of variable o1 is A and the class of the object referred to by o1 is C.
Similarly, class of variable o2 is B and the class of the object referred to by o2 is C. You are assigning o1 to o2, and o1 is pointing to an object of class C. Casting a variable to another type doesn't change the object. The object always remains what it is. Casting doesn't change the type of the variable either. Casting only tells the compiler that at run time the variable being casted will refer to an object of an appropriate class and so it should accept assignment.

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 » Mon Apr 17, 2017 8:54 pm

Trying to understand concept behind the lines below:

(1) A o1 = new C( );
(2) B o2 = (B) o1;

In line 1 , no casting is used while line 2 is using a casting. I am wondering since Ao1 is pointing to object of C, why it can't be assigned to the the reference Bo2 without a cast, since "C is a B". Just like in line 1 where "C is A" so no type casting used there. Any clarification will be appreciated.

admin
Site Admin
Posts: 7551
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: 7551
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: 21
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: 7551
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: 21
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: 7551
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: No registered users and 11 guests