About Question enthuware.ocajp.i.v7.-2-.1291 : about implict and explicit cast

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

Moderator: admin

Post Reply
flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

About Question enthuware.ocajp.i.v7.-2-.1291 : about implict and explicit cast

Post by flex567 »

System.out.println( ( (A) b ).fi ); What is this, upcasting?

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

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

Post by admin »

flex567 wrote:
Sun Jun 24, 2018 6:14 am
System.out.println( ( (A) b ).fi ); What is this, upcasting?
Yes, because you are casting a reference of a subtype to a supertype.
Try this if you are not clear - https://stackoverflow.com/questions/234 ... t-to-class

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

I think I understand:

A a = new B(); //upcasting

B b = new B();
a = (A) b;//also upcasting
(A) b;//also upcasting
Last edited by flex567 on Sun Jun 24, 2018 7:41 am, edited 2 times in total.

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

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

Post by admin »

There is no cast in A a = new b(); ! Also it should new B() not new b.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

It is implicit upcast.

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

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

Post by admin »

This is implicit "widening" not implicit upcast. There is nothing like implicit or explicit cast. Only cast. Cast is done using the cast operator i.e. ( ). If there is no cast operator, there is no cast.


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

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

Post by admin »

This is that particular author's view. You can take it up with him/her. There is no such term as implicit or explicit cast in the Java language specification. The term implicit cast or explicit cast is nonsensical. Either there is a cast or there is not. There is widening and widening can be explicit (i.e. using a cast) or implicit (i.e. without use a cast).

By the way, the book you quoted seems to be about Spring so I don't think the author is paying too much attention about such small technicality that has nothing to do with spring.

-Paul.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

I noticed it on other pages as well.

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

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

Post by admin »

You can talk to the author or ask someone else if you are not satisfied. Our view on this is quite clear.

HTH,
Paul.

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

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

Post by admin »

This discussion has nothing to do with the exam so I am splitting it into a separate topic.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests