Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2009
    Posts
    14
    Rep Power
    0

    Default anonymous (unnamed) Classes by default always folllows Overriding concept

    hi all,

    my question is related to anonymous (unnamed) classes.
    Anonymous classes are always sub-class of the some "new ClassName()". so my question is that if it is always sub-class of some class and if it is define the method that is already defined in super class then it is overriding the method. or we can say that it is by default follow the overriding principle.

    I need your thoughts on this Topic.

    Example Attached from SCJP Kathy Siera Book.

    class Popcorn {
    public void pop() {
    System.out.println("popcorn");
    }
    }
    class Food {
    Popcorn p = new Popcorn() {
    public void pop() {
    System.out.println("anonymous popcorn");
    }
    };
    }

    The Popcorn reference variable refers not to an instance of Popcorn, but to an
    instance of an anonymous (unnamed) subclass of Popcorn.
    Let's look at just the anonymous class code:
    2. Popcorn p = new Popcorn() {
    3. public void pop() {
    4. System.out.println("anonymous popcorn");
    5. }
    6. };

    Regards,
    Mahendra Athneria
    Mumbai- India

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    Cross post asked and answered forums.sun.com/thread.jspa?messageID=10620074

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Anonymous classes follow the same rules as named classes. They inherit from the base class or interface, they can override non-final methods, and they can declare new methods.

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    While it's true that an anonymous class can declare a new method, that new method can only be accessed within the anonymous class or it's inner classes, even if it's public.

    ...except by reflection. There is some crazy crap code out there, I tell you what.
    Last edited by toadaly; 02-26-2009 at 07:45 AM. Reason: oops, meant inner classes, not subclasses

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Toadaly is right about defining new methods. I was making a general observation, but in practice, I don't remember seeing it done.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    Part of the problem about new methods in an anonymous class is that the class itself becomes longer and more complex - so that it is less likely to be a candidate being anonymous.

    But what do subclasses of an anonymous class look like? Maybe I'm not thinking straight, but I couldn't figure out what toadaly was getting at there.

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Extending an anonymous class? Who started this discussion, anyway? Oh, yeah... All I meant to get at is that anonymous classes don't follow any special rules regarding methods. In practice, I use them sparingly, usually just to invoke a method of the containing class...

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Quote Originally Posted by pbrockway2 View Post
    But what do subclasses of an anonymous class look like? Maybe I'm not thinking straight, but I couldn't figure out what toadaly was getting at there.
    Well, toadaly couldn't figure it out either. He meant inner classes, and typed subclasses. ...corrected above.

Similar Threads

  1. Anonymous classes ...
    By Nicholas Jordan in forum Advanced Java
    Replies: 6
    Last Post: 11-27-2008, 07:02 PM
  2. Replies: 3
    Last Post: 04-08-2008, 08:12 AM
  3. is overriding static method possible
    By raghu in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 01:38 AM
  4. Name of Anonymous class
    By eva in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 02:07 PM
  5. Anonymous class
    By ravian in forum Advanced Java
    Replies: 3
    Last Post: 12-25-2007, 11:56 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
  •