About Question enthuware.ocajp.i.v7.-2-.1291 : about implict and explicit cast
Moderator: admin
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
About Question enthuware.ocajp.i.v7.-2-.1291 : about implict and explicit cast
System.out.println( ( (A) b ).fi ); What is this, upcasting?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
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
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
I think I understand:
A a = new B(); //upcasting
B b = new B();
a = (A) b;//also upcasting
(A) b;//also upcasting
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.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
There is no cast in A a = new b(); ! Also it should new B() not new b.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
It is implicit upcast.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
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.
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.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
I noticed it on other pages as well.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
You can talk to the author or ask someone else if you are not satisfied. Our view on this is quite clear.
HTH,
Paul.
HTH,
Paul.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1291 :
This discussion has nothing to do with the exam so I am splitting it into a separate topic.
Who is online
Users browsing this forum: No registered users and 10 guests