-1

I have a basic question in java. from my research I found out that downcasting is not allowed in java because it throws an exception in runtime but in some limited cases such as Downcasting in Java

But I can't understand why this statement proceeds HttpURLConnection connection = (HttpURLConnection) url.openConnection() without problem?

(because openConnection() returns a URLConnection object )

be specific

I'm sorry for poor english language because i am not a native speaker

Community
  • 1
  • 1
Mostafa zamani
  • 78
  • 1
  • 12
  • thanks yassin; but under which circumstances it returns true or false – Mostafa zamani Apr 03 '16 at 09:52
  • I closed your question as a duplicate of the exact one you mention because the answer is literally the first line in the first answer: "*Downcasting is allowed when there is a possibility that it suceeds at run time*". Contrast this to your statement "*downcasting is not allowed in java because it throws an exception in runtime*" and it seems like a simple mistake in interpretation. – Jeroen Vannevel Apr 03 '16 at 09:56
  • @JeroenVannevel if it is so why sometimes downcasting throws classcastexception? – Mostafa zamani Apr 03 '16 at 10:11
  • Just because there is a possibility it succeeds doesn't mean there is no possibility it doesn't succeed. Downcasting is allowed when it "may or may not" work and when it doesn't work, you get the exception. – Jeroen Vannevel Apr 03 '16 at 10:12

2 Answers2

0

Because the object returned from url.openConnection in the circumstances it is used here is a HttpUrlConnection and the programmer needs access to its methods, but it was returned as an interface, so javac has no idea that it is anything else but the interface.

In other words, the programmer breaks the encapsulation design in the API to get access to functionality hidden by the API.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
  • thank you. can you give some scenario[guidelines] when java has problem with downcasting and when not?? – Mostafa zamani Apr 03 '16 at 10:49
  • You must understand the role that _interfaces_ have in Java. An example: An interface could be a "Car". You could then have two classes "BMW" and "VW" which each are a Car. A BMW is not a VM and a VW is not a BMW. If you have a statement like "Car car = getCar()" it could not return _just_ a Car (because that is the interface) but _must_ return a BMW or a VW (or whatever else you have). Downcasting means "from now on treat as a". You cannot treat a VW as a BMW. – Thorbjørn Ravn Andersen Apr 03 '16 at 10:55
0

URL.openConnection() returns a URLConnection, which is an abstract class, i.e. the returned object can never really be of type URLConnection, but must be of a class that inherits from URLConnection, such as HttpURLConnection.

As the accepted answer to the Stackoverflow thread you linked to says,

Downcasting is allowed when there is a possibility that it succeeds at run time:

In this case, it is guaranteed to succeed at runtime, because the returned object can not be an instance of the abstract class URLConnection as explained above.

Magnus
  • 17,157
  • 19
  • 104
  • 189