This discussion is archived
11 Replies Latest reply: Dec 17, 2010 7:02 PM by EJP RSS

Problems adding new method to abstract class

824702 Newbie
Currently Being Moderated
In the program class:
...
URL url = new URL("http://www.google.com");
extURLConnection c = new extURLConnection(url.openConnection());
...
if (c.isConnected){
...

In URLConnection I cant access the attribute connected (that shows if i got a connection or not). so I want to add a method that return that attribute.

So I created a class called extURLConnection that extends URLConnection. As method connect() is abstract I must define this class as abstract. And this is here I get problems. Cause of this i cant initiate this class (as i written above). So how do I implement this isConnected method? Is it possible to do it in another way? I've read some thing about Interfaces, can it be done with that?

import java.net.*;
class DpURLConnection extends URLConnection{
DpURLConnection(URLConnection URLConn){
super(URLConn.getURL());
}

public boolean isConnected(){
return this.connected;
}
}

Edited by: user13496675 on Dec 16, 2010 5:36 PM
  • 1. Re: Problems adding new method to abstract class
    EJP Guru
    Currently Being Moderated
    In URLConnection I cant access the attribute connected (that shows if i got a connection or not).
    You don't need to. Either you got the URLConnection or you didn't. And when you did, it still wasn't connected. That only happens when you do I/O. And when you do that either it throws an exception or it doesn't. If it doesn't, you are connected.
  • 2. Re: Problems adding new method to abstract class
    824702 Newbie
    Currently Being Moderated
    I'm streaming all data from the site into a buffert. I wanted to make sure the connection was made before doing a buffert. But i guess i can do several try{ } catch on each segment of code. the thing was that i wanted to print a status if it is connected or not.
    so should i implement the method that uses the connection, or should i just make a try catch, and if it catches any error from the connection, then print out connection failed.
  • 3. Re: Problems adding new method to abstract class
    EJP Guru
    Currently Being Moderated
    The answer to this type of question is always to just attempt the operation and deal with the failure if it happens. Trying to guess ahead of time whether it will succeed is trying to predict the future.
  • 4. Re: Problems adding new method to abstract class
    824702 Newbie
    Currently Being Moderated
    Error handling is a part of coding. If the page is going down, no connection will be done. Therefore some sort of feedback should be sent to the user.
  • 5. Re: Problems adding new method to abstract class
    EJP Guru
    Currently Being Moderated
    Error handling is a part of coding.
    Of course it is. What we are discussing is how to do it. You do it when you get the error. In this case, when you get the exception .
  • 6. Re: Problems adding new method to abstract class
    824702 Newbie
    Currently Being Moderated
    So what are you saying. I should use a try and catch?

    I think you should first write a code that avoids exceptions, like the way I asked help for above.
  • 7. Re: Problems adding new method to abstract class
    Kayaman Guru
    Currently Being Moderated
    user13496675 wrote:
    So what are you saying. I should use a try and catch?
    You'll need to use it anyways. Unless you intend to implement poor error handling.
    I think you should first write a code that avoids exceptions, like the way I asked help for above.
    It's not always possible to avoid exceptions. Like in this case. You seem to be thinking that creating an (Http)URLConnection connects to the website. It doesn't, so no matter how you implement it you won't get the functionality that you're asking for.
  • 8. Re: Problems adding new method to abstract class
    EJP Guru
    Currently Being Moderated
    So what are you saying. I should use a try and catch?
    When dealing with exceptions you don't have much choice.
    I think you should first write a code that avoids exceptions, like the way I asked help for above.
    I don't know where you got this idea from but it is complete nonsense. The JDK will throw exceptions at you, and the compiler will oblige you to write catch clauses for them. You may as well put something useful into them. That's what exceptions are for. You can't 'avoid' them. And you certainly can't predict the future such that you go around all the ways in which they are going to occur.

    I am also baffled what all this has to do with your title.
  • 9. Re: Problems adding new method to abstract class
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    user13496675 wrote:
    So what are you saying. I should use a try and catch?

    I think you should first write a code that avoids exceptions, like the way I asked help for above.
    Except your code doesn't.

    You want to ask the connection if it is good and THEN use the connection.

    What happens if someone kicks out the power plug on the server between your first action and your second?

    And what exactly do think that normally would happen anyways? Do you expect that 99.99% of the time that the target will in fact be up? So to test a connection (which requires that you send data) you would be adding 10,000 messages to the network to detect one failure. A failure that you could detect just as easily by sending the message. Does that really seem like a good idea?
  • 10. Re: Problems adding new method to abstract class
    824702 Newbie
    Currently Being Moderated
    If there is an established connection, then start streaming data. That is what i want to do. If there is no connection, then retry connecting x times then send a feedback to the user that it is not possible to connect. And this could be done with try catch. So I dont see the point in your answer if you dont have anything decent to write. What you wrote has already been pointed out.
  • 11. Re: Problems adding new method to abstract class
    EJP Guru
    Currently Being Moderated
    You don't seem to understand. The way the API is designed, you can only know if the connection is there by trying to use it. And even if it was known to be there when you started, you can still get exceptions while writing to it. So you have to cope with those anyway. So that's the only mechanism you need.

    I'll give you another example:
    void writeToFile(byte[] data, File file)
    {
      if (!file.canWrite())
      {
        System.out.println("file cannot be written to - "+file);
        return;
      }
      try
      {
        FileOutputStream out = new FileOutputStream(file);
        out.write(data);
        out.close();
      }
      catch (IOException exc)
      {
        System.out.println("file cannot be written to - "+file);
        return;
      }
    }
    You have to write the catch block anyway, ,and put something sensible into it. So the 'if' statement is completely pointless. So don't write it. Writing code like this also introduces unnecessary points of failure: what if the file wasn't writable when the 'if' was executed but then became writable before the new FileOutputStream()? What if it became unwritable afterwards? It is also wasteful: new FileOutputStream has to check whether the file is writable anyway, so why check it twice?

    The same arguments apply to your post. The API doesn't actually open a TCP connection until you do some I/O. So you have to do some I/O. So you have to handle exceptions. So you don't have to do anything else about failure to connect.
    So I dont see the point in your answer if you dont have anything decent to write. What you wrote has already been pointed out.
    I don't see any point whatsoever in being rude to people who are trying to help you, gratis, out of their many years of experience. You are being told the same thing by more than one person. That should cause you to think rather than reach for the rude button.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points