Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java Protected access problem

  • 08-10-2005 02:27PM
    #1
    Registered Users, Registered Users 2 Posts: 1,322 ✭✭✭


    Im trying to use a protected attribute i seen in the URLConnection class but because its protected it wont compile for me.

    The attribute is the protected connected attribute that returns a boolean, true if the connection is made etc...

    How do i access this method as it would be really handy for the project.

    The actual compiler error is
    " connected has protected access in java.net.URLConnection"

    any help would be great.


Comments

  • Registered Users, Registered Users 2 Posts: 1,272 ✭✭✭i_am_dogboy


    Protected attributes and methods are only available to the class itself, classes in the same package of sub classes. I've never used URLConnection before so I don't know how it works, but I'm guessing the method is declared protected for a good reason, if you want access extend the URLConnection class.


  • Registered Users, Registered Users 2 Posts: 885 ✭✭✭clearz


    Extend the URLConnection class and in your new class create a public method that calls the protected method. Then use your new clanss wherever you are using the URLConnection class in your main code.

    And yes its probably protected for a good reason but thats how to get arround it anyway.


  • Registered Users, Registered Users 2 Posts: 1,322 ✭✭✭Mad_Max


    Thanks for the advice. I couldnt actually get that to work for some reason it wouldnt let me extend the URLConnection class..

    However i found another way of checking the connections by requesting the headers and then going from there....seems to work ok now.

    Thanks again for the help.


  • Closed Accounts Posts: 324 ✭✭madramor


    public class MyURLConnection extends URLConnection{
        
        public MyURLConnection(URL u){
            super(u);
        }
        public void connect() throws IOException{
        }
        public boolean isConnected(){
            return this.connected;
        }
    }
    


Advertisement