Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #22374 > unrolled thread
| Started by | bob smith <bob@coolfone.comze.com> |
|---|---|
| First post | 2013-02-19 13:45 -0800 |
| Last post | 2013-02-20 22:05 -0800 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
HttpURLConnection bob smith <bob@coolfone.comze.com> - 2013-02-19 13:45 -0800
Re: HttpURLConnection Lew <lewbloch@gmail.com> - 2013-02-19 14:31 -0800
Re: HttpURLConnection Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-02-19 17:56 -0500
Re: HttpURLConnection bob smith <bob@coolfone.comze.com> - 2013-02-20 10:26 -0800
Re: HttpURLConnection Arne Vajhøj <arne@vajhoej.dk> - 2013-02-22 13:28 -0500
Re: HttpURLConnection Roedy Green <see_website@mindprod.com.invalid> - 2013-02-20 22:05 -0800
| From | bob smith <bob@coolfone.comze.com> |
|---|---|
| Date | 2013-02-19 13:45 -0800 |
| Subject | HttpURLConnection |
| Message-ID | <30772112-527f-47b0-9333-70777b889b5c@googlegroups.com> |
How can people use the class HttpURLConnection when the abstract method connect() from URLConnection is never defined? I thought you can't use a class till all the abstract blanks are filled in?
[toc] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2013-02-19 14:31 -0800 |
| Message-ID | <a4077e75-4640-44be-a415-44fbf0fa5f48@googlegroups.com> |
| In reply to | #22374 |
On Tuesday, February 19, 2013 1:45:25 PM UTC-8, bob smith wrote: > How can people use the class HttpURLConnection when the abstract method connect() > from URLConnection is never defined? False. > I thought you can't use a class till all the abstract blanks are filled in? True. Look up polymorphism and widening conversions. http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.5 Understand the difference between declared type (compile time) and run-time type. Read the Java tutorials. Did you know that any object of a given type is also an object of every supertype of its type? That's how you can get List<Foo> foos = new ArrayList<>(); 'List', being an interface, has only abstract instance methods. Yet somehow you can call foos.add(new Foo()); Same thing. This is basic Java stuff. Heck, it's basic O-O stuff. Google around for some introductory texts. -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2013-02-19 17:56 -0500 |
| Message-ID | <kg0vtf$jap$1@dont-email.me> |
| In reply to | #22374 |
On 2/19/2013 4:45 PM, bob smith wrote:
> How can people use the class HttpURLConnection when the abstract method connect() from URLConnection is never defined?
>
> I thought you can't use a class till all the abstract blanks are filled in?
The openConnection() method of the URL class returns a
URLConnection object. I haven't used it myself, but from the
documentation it appears that for an HTTP URL the URLConnection
will in fact be an HttpURLConnection.
Both of those classes are abstract, and as such they cannot
be instantiated. The object actually returned will be an instance
of some concrete subclass, possibly anonymous. The inheritance
tree would look something like
java.lang.Object
java.net.URLConnection
java.net.HttpURLConnection
(maybe a few more levels here)
some.concrete.class.Thing
If you're curious, you can do getClass() on the object you
get back from openConnection(), and print its class name or do
other snoopy things. But to use it, you're just fine treating
it as an HttpURLConnection -- because it "is an" HttpURLConnection,
in exactly the same way that an Integer "is a" Number.
--
Eric Sosman
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | bob smith <bob@coolfone.comze.com> |
|---|---|
| Date | 2013-02-20 10:26 -0800 |
| Message-ID | <f59562c3-f72f-440c-a8de-4da5c31b0c2d@googlegroups.com> |
| In reply to | #22378 |
On Tuesday, February 19, 2013 4:56:31 PM UTC-6, Eric Sosman wrote: > On 2/19/2013 4:45 PM, bob smith wrote: > > > How can people use the class HttpURLConnection when the abstract method connect() from URLConnection is never defined? > > > > > > I thought you can't use a class till all the abstract blanks are filled in? > > > > The openConnection() method of the URL class returns a > > URLConnection object. I haven't used it myself, but from the > > documentation it appears that for an HTTP URL the URLConnection > > will in fact be an HttpURLConnection. > > > > Both of those classes are abstract, and as such they cannot > > be instantiated. The object actually returned will be an instance > > of some concrete subclass, possibly anonymous. The inheritance > > tree would look something like > > > > java.lang.Object > > java.net.URLConnection > > java.net.HttpURLConnection > > (maybe a few more levels here) > > some.concrete.class.Thing > > > > If you're curious, you can do getClass() on the object you > > get back from openConnection(), and print its class name or do > > other snoopy things. But to use it, you're just fine treating > > it as an HttpURLConnection -- because it "is an" HttpURLConnection, > > in exactly the same way that an Integer "is a" Number. > > > > -- > > Eric Sosman > > esosman@comcast-dot-net.invalid Thanks. This clears up a lot.
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2013-02-22 13:28 -0500 |
| Message-ID | <5127b8df$0$294$14726298@news.sunsite.dk> |
| In reply to | #22395 |
On 2/20/2013 1:26 PM, bob smith wrote:
> On Tuesday, February 19, 2013 4:56:31 PM UTC-6, Eric Sosman wrote:
>> On 2/19/2013 4:45 PM, bob smith wrote:
>>> How can people use the class HttpURLConnection when the abstract method connect() from URLConnection is never defined?
>>>
>>> I thought you can't use a class till all the abstract blanks are filled in?
>>
>> The openConnection() method of the URL class returns a
>> URLConnection object. I haven't used it myself, but from the
>> documentation it appears that for an HTTP URL the URLConnection
>> will in fact be an HttpURLConnection.
>>
>> Both of those classes are abstract, and as such they cannot
>> be instantiated. The object actually returned will be an instance
>> of some concrete subclass, possibly anonymous. The inheritance
>> tree would look something like
>>
>> java.lang.Object
>> java.net.URLConnection
>> java.net.HttpURLConnection
>> (maybe a few more levels here)
>> some.concrete.class.Thing
>>
>> If you're curious, you can do getClass() on the object you
>> get back from openConnection(), and print its class name or do
>> other snoopy things. But to use it, you're just fine treating
>> it as an HttpURLConnection -- because it "is an" HttpURLConnection,
>> in exactly the same way that an Integer "is a" Number.
>
> Thanks. This clears up a lot.
If you want to see everything:
import java.io.IOException;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLConnection;
public class WhatConnection {
private static final String INDENT = " ";
private static String getType(Class<?> clz) {
if(clz.isInterface()) {
return "interface";
} else if(clz.isEnum()) {
return "enum";
} else {
if((clz.getModifiers() & Modifier.ABSTRACT) != 0) {
return "abstract class";
} else {
return "class";
}
}
}
public static void dumpClassInfo(String indent, Class<?> clz) {
System.out.println(indent + getType(clz) + " " + clz.getName());
if(clz.getSuperclass() != null) dumpClassInfo(indent + INDENT,
clz.getSuperclass());
for(Class<?> intf : clz.getInterfaces()) {
dumpClassInfo(indent + INDENT, intf);
}
}
public static void test(String urlstr) throws IOException {
System.out.println(urlstr + ":");
URL url = new URL(urlstr);
URLConnection con = url.openConnection();
dumpClassInfo(INDENT, con.getClass());
}
public static void main(String[] args) throws Exception {
test("http://www.oracle.com/");
test("ftp://ftp.oracle.com/");
}
}
Arne
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2013-02-20 22:05 -0800 |
| Message-ID | <7tcbi8teekliep35tb5jh6kon5m7e5j2qd@4ax.com> |
| In reply to | #22374 |
On Tue, 19 Feb 2013 13:45:25 -0800 (PST), bob smith <bob@coolfone.comze.com> wrote, quoted or indirectly quoted someone who said : >How can people use the class HttpURLConnection when the abstract method connect() from URLConnection is never defined? > >I thought you can't use a class till all the abstract blanks are filled in? If you look at what URL.openConnection gives you with getClass to will see a variety of objects such as: HttpURLConnection HttpsURLConnectionImpl FileURLConnection JarURLConnection depending on which protocol you specified in the URL e.g. http:, https:, file:, jar: See http://mindprod.com/jgloss/http.html -- Roedy Green Canadian Mind Products http://mindprod.com The generation of random numbers is too important to be left to chance. ~ Robert R. Coveyou (born: 1915 died: 1996-02-19 at age: 80)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web