0

I am new to java and when i am searching over internet for getting the HTTP response code through java, i came to know about [HttpURLConnection] Class in java. This class is abstract class hence i cant instantiate this class. So my queestion is I have a URL object and how to use this abstract class in my code. This could be Very Basic Question but your answer would help to understand java fundamentally.

Thanks in Advance

https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html

sri ram
  • 9
  • 2
  • 6
  • By getting an instance of a concrete subclass. – SLaks Nov 29 '17 at 17:01
  • I have updated the java 8 link. @SLaks - My question is "is there way to user methods of Abstract class without using the class which extends the Abstract Class" – sri ram Nov 29 '17 at 17:05

4 Answers4

1

You get a HttpURLConnection object from a URL instance that represents an http: (or https:) protocol URL, from its openConnection method. What you get back is a reference to an object conforming to the interface defined by the URLConnection abstract class (but it'll be an instance of an HttpURLConnection subclass).

You often don't need to know the concrete class you're working with; you code to an interface that defines the things you should be able to do with what you have. Those things then get done by the concrete class of the object you have a reference to.

Re this specifically:

is there way to user methods of Abstract class without using the class which extends the Abstract Class

Not if they're instance methods, no. The clue is in the name. ;-) To use an instance method, you must have an instance. To have an instance, you must have a concrete (non-abstract) class that the instance is a member of.

But again: Often you don't care what that class is, just that you have an instance conforming to the interface you need to use.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

You can't instantiate an abstract class needs an subclass to implement specific methods.

You can access its method with an anonymous class for example:

new HttpURLConnection() { /* here you need to implement many empty methods */ }.getOutputStream();

But for your problem, I think you want a real HttpURLConnection that can be acquired by the URL object.

HttpURLConnection connection = urlObject.openConnection();
connection.setDoInput(true);
connection.setDoOutput(false);
// input = true & output = false means a GET
// input = true & output = true means a POST
connection.connect();
InputStream content = connection.getInputStream();
Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

You have to use a concrete class which is sub class of the abstract class to call the method.

Or if the function you want to use is static, you can call it directly. Below is a very small example to show it.

abstract class SimpleAbstract {
public static String testString() {
    return "You got me";
}

public abstract void doMagic();}
public class AbstractExample {
public static void main(String[] args) {
    System.out.println(SimpleAbstract.testString());        
}}
SK -
  • 459
  • 5
  • 15
0

Here is a example to intialize abstract class without extending it

    abstract class Abstractclass{
       public void someMethod(){
          System.out.println("Hello");
       }
       abstract public void anotherMethod();
    } 
    public class Demo {
       public static void main(String args[])
       { 
         //creation of object .........................

          Abstractclass obj = new Abstractclass(){
              public void anotherMethod(){
                 System.out.println("Hello12222");
                              }
                };    
         obj.anotherMethod();
         obj.someMethod();
       }
    }

output : Hello12222 Hello

quinz
  • 1,282
  • 4
  • 21
  • 33
  • output : Hello12222 Hello – vishnu singh Aug 07 '18 at 07:43
  • In your above code , you instantiated an object from a abstract class **Abstractclass**. It would really help if you could explain it out. – sri ram Oct 06 '18 at 16:26
  • Here , what i did is just defined the abstract function with the intialisation of abstract class object. That's what it requires while we extend it in sub class and then defines the abstract function ... – vishnu singh Oct 07 '18 at 17:19