This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Problem with HttpURLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with HttpURLConnection" Watch "Problem with HttpURLConnection" New topic
Author

Problem with HttpURLConnection

Karthik Vaidiswaran
Greenhorn

Joined: Jan 11, 2005
Posts: 16
I call openConnection() method on a URL object. I try to assign this to HttpURLConnection reference ,

url = URL(...); //LINE 1
HttpURLConnection huc = url.openConnection(); //LINE 2

I am getting an error which says

incompatible types
found : java.net.URLConnection
required: java.net.HttpURLConnection
huc = url.openConnection();

When I do System.out.pritln(url.openConnection().getClass()), I get something like
classs sun.net.www.protocol.http.HttpURLConnection.

I expected the class to be of type java.net.HttpURLConnection. Please tell me why the class is different. Please help me in fixing the error I get in line 2 of code

Thanks
Karthik
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

try this...


con.openConnection() returns an object of type URLConnection. Now URLConnection is an abstract class and what it actually returns is an object of a concrete sub class. sub-class objects can be referenced by super class variable in this fashion.



When u have a variable of type subclass and you require that variable to point to the subclass object (currently pointed to by the superclass variable), you have to specifically cast it like this



for more info
http://java.sun.com/docs/books/tutorial/java/javaOO/subclasses.html

tx,
ram.
Karthik Vaidiswaran
Greenhorn

Joined: Jan 11, 2005
Posts: 16
Hi Ram,

Thanks for the reply.


Regards
Karthik
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
A better solution (in my opinion) is to change the type of your variable:

One purpose of the superclass-subclass relationship is to provide an abstract interface through the superclass where each subclass provides the details for a specific implementation. Unless you absolutely need methods that are provided only by the subclass, you should use superclass references rather than subclass references.

This is a very fundamental concept in object-oriented design and programming. It would definitely be beneficial for you to study up on inheritence and related topics (such as polymorphism).

Keep Coding!

Layne


Java API Documentation
The Java Tutorial
 
 
subject: Problem with HttpURLConnection
 
Similar Threads
simple web service client
How to make a 'post' request from an application to a Servlet
Reading hyperlink in excel through Java
About HttpurlConnection.java
Access to website with proxy using Java Code