Dont understand the concept of extends in URL.openConnection() in JAVA

Posted by user1722361 on Stack Overflow See other posts from Stack Overflow or by user1722361
Published on 2012-10-06T23:27:59Z Indexed on 2012/10/07 3:38 UTC
Read the original article Hit count: 97

Filed under:
|
|
|

Hi I am trying to learn JAVA deeply and so I am digging into the JDK source code in the following lines:

URL url = new URL("http://www.google.com");
URLConnection tmpConn = url.openConnection();

I attached the source code and set the breakpoint at the second line and stepped into the code. I can see the code flow is: URL.openConnection() -> sun.net.www.protocol.http.Handler.openConnection() I have two questions about this

First In URL.openConnection() the code is:

public URLConnection openConnection() throws java.io.IOException {
        return handler.openConnection(this);
    }

handler is an object of URLStreamHandler, define as blow

transient URLStreamHandler handler;

But URLStreamHandler is a abstract class and method openConnection() is not implement in it so when handler calls this method, it should go to find a subclass who implement this method, right? But there are a lot classes who implement this methods in sun.net.www.protocol (like http.Hanlder, ftp.Handler ) How should the code know which "openConnection" method it should call? In this example, this handler.openConnection() will go into http.Handler and it is correct. (if I set the url as ftp://www.google.com, it will go into ftp.Handler) I cannot understand the mechanism.

second. I have attached the source code so I can step into the JDK and see the variables but for many classes like sun.net.www.protocol.http.Handler, there are not source code in src.zip. I googled this class and there is source code online I can get but why they did not put it (and many other classes) in the src.zip? Where can I find a comprehensive version of source code?

Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about eclipse