Results 1 to 8 of 8
  1. #1
    gillbates is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Abstract Classes and subclassing anonymously

    I've been confused about this since I started digging into Java. I know you cannot instantiate an abstract class. I went through this thread and see that you can insert an anonymous class into another class that implements the abstract class and then run the concrete methods in the abstract class through there. I suppose that's one way to do it. The specific thing that confuses me is when I run into code like the following:

    Java Code:
    String urltext = "http://www.theagitator.com";
    		URL url = new URL(urltext);
    		String contentType = ((HttpURLConnection) url.openConnection())
    				.getContentType();
    		System.out.println(contentType);
    URL is a public final class and its openConnection method returns a URLConnection, which is then used to access the getContentType method of the public abstract class URLConnection. Now the only abstract method is connection, and it says that it's happy to take a connection from elsewhere, so implementing URLConnection should be easy, but I don't get where that happens. It seems to me that URLConnetion is going to have to get instantiated somewhere, in order to get to the getContentType function.

    Can someone explain what makes the method usable? Also, how do you figure out that you can use an abstract library like this? I wrote test code and it will not let you do that with a homegrown abstract class. At least not unless you do the anonymous class trick:


    Java Code:
    public abstract class myab {
      int a;
      public myab(){};
    
      public myab(int a) {
        this.a = a;
      }
    
      public abstract int retone(int a);
    
      public int rettwo(int a) {
        System.out.println(a);
        return 2;
      }
    }
    class useit {
       public myab retit(int a) {
          System.out.println(new myab(3){public int retone(int a){return 6;} }.getClass().getName());
          return (new myab(3){public int retone(int a){return 6;} });
       }
    }
    class test {
    
      public static void main(String[] args) {
        useit u = new useit();
        int z = (u.retit(5).rettwo(8));
        System.out.println(" output from two = " + z);
        System.out.println(new Object() {}.getClass().getName());
        System.out.println(u.getClass().getName());
        System.out.println("myab " + u);
      }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Abstract Classes and subclassing anonymously

    Removed from Creating objects of Abstract Classes ?

    When you have a question, start your own thread. Don't waken an old dead thread and don't hijack another poster's thread, especially not with a question that is only tenuously related.

    If you want to get better help, edit your post and format that code according to some standard convention. It's tedious to read a run of nested blocks all on one line, so most members won't bother.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Abstract Classes and subclassing anonymously

    If something claims it returns an object of type B while it returns an object of type D where D extends B, that something wasn't lying or cheating because a D is a B.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    gillbates is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Abstract Classes and subclassing anonymously

    OK, but I'm not sure why my post was only tenuously related. His question was, why am I able to create an object from an abstract class, and the answer was he had not, because he had created an anonymous class that defined the abstract class when he instantiated it.

    My question is, how do you use URLConnection.getContentType() (an abstract class) without instantiating it? I see things like that in java code, fairly often, and was hoping someone could explain how that works.

    I just threw that test code in the bottom to show that I had put some effort in, and to help people that came along later and wanted to understand how the anonymous class stuff worked. I guess I should have pointed that out so people wouldn't think it was part of the question.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Abstract Classes and subclassing anonymously

    So, Jos pointed out the answer to your question.

    Just because something returns a URLConnection doesn't mean it's literally returning the abstract URLConnection class, rather, it is returning an instance of a class that extends URLConnection. Because of generalization, any class that extends URLConnection is a URLConnection, but the instance itself was created from a subclass.

    Abstract classes can behave something like interfaces in the sense that they define what you can use/expect but you cannot use the directly. However, they differ from interfaces in that interfaces only give you the what and not the how. Abstract classes can define method bodies. One then either extends the class and overrides things, or adds things to make it functional.

    There are many cases where some class is the root of an entire data structure for example, and as such, contains methods all children will inherit, however, it doesn't make sense to ever use the root class directly, as it's included methods are not sufficient to do anything useful.

    I hope that helps explain it a bit better?

  6. #6
    gillbates is offline Member
    Join Date
    Apr 2012
    Posts
    12
    Rep Power
    0

    Default Re: Abstract Classes and subclassing anonymously

    Thanks for the response. Let me see if I understand. In this case:

    Java Code:
         String urltext = "http://www.theagitator.com";
         URL url = new URL(urltext);
         int responseCode = ((HttpURLConnection) url.openConnection())
                         .getResponseCode();
         System.out.println("urlconnection " + ((HttpURLConnection) url.openConnection()));
    We create an object of type URL. Because HttpURLConnection is a subclass of URL, it is a URL, so any methods in HttpURLConnection (or URLConnection) can be called against a URL object (after casting the object).

    If that's the case, many thanks for finally clearing this up for me. One additional question. What happens when a method in the abstract subclass uses a property that's defined in the abstract subclass? Couldn't that cause a problem? Is there someway to tell which subclass methods can be used on a superclass's object?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Abstract Classes and subclassing anonymously

    Quote Originally Posted by gillbates View Post
    What happens when a method in the abstract subclass uses a property that's defined in the abstract subclass? Couldn't that cause a problem? Is there someway to tell which subclass methods can be used on a superclass's object?
    If it would cause trouble, that class would be ill designed.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Abstract Classes and subclassing anonymously

    I agree with Jos. You can make things private if they are not to be used!

Similar Threads

  1. Help with Abstract Classes please.
    By fatabass in forum New To Java
    Replies: 1
    Last Post: 03-23-2012, 04:26 AM
  2. Abstract classes
    By FadedAura in forum New To Java
    Replies: 4
    Last Post: 11-21-2011, 11:16 PM
  3. Abstract Classes and ArrayLists.
    By Hooky75 in forum New To Java
    Replies: 9
    Last Post: 05-14-2011, 10:38 AM
  4. Abstract Classes.
    By maknib in forum New To Java
    Replies: 3
    Last Post: 05-12-2011, 03:30 PM
  5. abstract classes
    By renju krishnan in forum New To Java
    Replies: 1
    Last Post: 09-29-2010, 09:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •