0

I was looking up sample codes that were previously written by colleague who left the firm. But am not able to understand what does defining a method with an Abstract class name achieve

This is the import statement

import java.net.HttpURLConnection;

And there is a method written as

private static HttpURLConnection pullData(url, redirects) throws IOException
try
{
String method = "GET";
HttpURLConnection connection = null;
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method);
connection.setInstanceFollowRedirects(false);
int statusCode = connection.getResponseCode();
if (statusCode == 301) {
                if (redirects == 0) {
                    throw new IOException("Stop!");
                }
                connection.disconnect();
                return pullData(url, redirects - 1);
}
catch (IOException ioerror) {
            if (connection != null) {
                connection.disconnect();
            }
            throw ioerror;
}
        return connection;
}

Now i was reading up on HttpURLConnection on https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html and noticed it is an abstract class. But have not understood what does defining a method with that class name achieves. Is it supposed to be like a data type?

Basically what does private static HttpURLConnection <methodname> do?

user3423407
  • 341
  • 3
  • 13
  • 1
    It means that the method will return an object which is an instance of the `HttpURLConnection` class. It's unclear what confuses you. – LEQADA Jul 15 '20 at 00:40
  • Thanks @LEQADA - "return an object which is an instance of the HttpURLConnection class" - this is what I was trying to understand. Usually one would create an object by instantiation of a Class but in this case it is returning an object – user3423407 Jul 15 '20 at 01:03
  • All that is happening is that 'someone else' is actually creating the object - quite likely `url.openConnection()`. – user13784117 Jul 15 '20 at 12:08

1 Answers1

3

A class is a data type, not like a data type.

Writing a method like

       DataTypeName methodName(…) { … }

says that methodName returns a reference to an object of type DataTypeName.

It's not really relevant to that definition whether DataTypeName is a superclass of some other class, nor whether it is declared abstract.

The use of abstract simply means that the abstract class must be subclassed; you cannot instantiate an object of an abstract class.

In your example, all it means is that there could several different subclasses, but this routine treats them all like they are of type HttpURLConnection. That's simple object-oriented polymorphism.

user13784117
  • 1,124
  • 4
  • 4
  • _"A class **is** a data type, not **like** a data type."_ I think I know what you mean, but... your answer would be improved if you removed that sentence. I like the description of classes given [here](https://docs.oracle.com/javase/tutorial/java/concepts/class.html): _A class is the blueprint from which individual objects are created._ Java has _types_ (rather than _data_ types), namely, primitive types and reference types. There is a nice summary here: [difference between class and type](https://stackoverflow.com/questions/16600750/difference-between-class-and-type). – andrewJames Jul 15 '20 at 01:56