Page 1 of 1

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

Posted: Sun Jun 24, 2018 6:14 am
by flex567
System.out.println( ( (A) b ).fi ); What is this, upcasting?

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

Posted: Sun Jun 24, 2018 6:24 am
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

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

Posted: Sun Jun 24, 2018 6:32 am
by flex567
I think I understand:

A a = new B(); //upcasting

B b = new B();
a = (A) b;//also upcasting
(A) b;//also upcasting

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

Posted: Sun Jun 24, 2018 6:37 am
by admin
There is no cast in A a = new b(); ! Also it should new B() not new b.

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

Posted: Sun Jun 24, 2018 7:41 am
by flex567
It is implicit upcast.

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

Posted: Sun Jun 24, 2018 8:25 pm
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.

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

Posted: Mon Jun 25, 2018 2:51 am
by flex567

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

Posted: Mon Jun 25, 2018 4:19 am
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.

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

Posted: Mon Jun 25, 2018 4:35 am
by flex567
I noticed it on other pages as well.

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

Posted: Mon Jun 25, 2018 4:37 am
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.

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

Posted: Mon Jun 25, 2018 4:39 am
by admin
This discussion has nothing to do with the exam so I am splitting it into a separate topic.