anonymous class

Discussion in 'Java' started by josh, Mar 26, 2007.

  1. josh

    josh Guest

    Hi If I have

    Shape2D -> Interface
    Shape3D -> abstract class

    Can I do an anonymous class only with these two class-typo?

    i.e.

    new Shape2D() {} // here is equals, as if I did, to class MyClass
    implements Shape2D () {}

    new Shape3D() {} // here is equals as If I did, to class MyClass
    extends Shape3D() {}
     
    josh, Mar 26, 2007
    #1
    1. Advertising

  2. josh

    Tom Hawtin Guest

    josh wrote:
    >
    > Shape2D -> Interface
    > Shape3D -> abstract class
    >
    > Can I do an anonymous class only with these two class-typo?


    You want an anonymous inner class that is a subtype of two types?

    There is no syntax for that, and it probably means you are doing
    something a bit complex for anonymous inner classes.

    If you really want to do it, you can introduce a local class to combine
    the two types:

    abstract class Shape2D3D extends Shape3D implements Shape2D {
    }
    ... new Shape2D3D() {
    ...
    }:

    Although, I can't say I've ever felt the need.

    Tom Hawtin
     
    Tom Hawtin, Mar 26, 2007
    #2
    1. Advertising

  3. josh

    josh Guest

    On 26 Mar, 14:36, Tom Hawtin <> wrote:
    > josh wrote:
    >
    > > Shape2D -> Interface
    > > Shape3D -> abstract class

    >
    > > Can I do an anonymous class only with these two class-typo?

    >
    > You want an anonymous inner class that is a subtype of two types?


    sorry I explain myself bad...

    I wanted say if I can create anonymous class only for interface OR
    abstract class
     
    josh, Mar 26, 2007
    #3
  4. Hi josh,

    josh wrote:
    >>>Shape2D -> Interface
    >>>Shape3D -> abstract class

    >>
    >>>Can I do an anonymous class only with these two class-typo?

    >>
    >>You want an anonymous inner class that is a subtype of two types?

    >
    > sorry I explain myself bad...
    >
    > I wanted say if I can create anonymous class only for interface OR
    > abstract class


    I do not understand what you mean, as well.

    What would be the third possibilty if not a class nor an interface?

    Ciao,
    Ingo
     
    Ingo R. Homann, Mar 26, 2007
    #4
  5. josh

    Chris Dollin Guest

    Ingo R. Homann wrote:

    > Hi josh,
    >
    > josh wrote:
    >>>>Shape2D -> Interface
    >>>>Shape3D -> abstract class
    >>>
    >>>>Can I do an anonymous class only with these two class-typo?
    >>>
    >>>You want an anonymous inner class that is a subtype of two types?

    >>
    >> sorry I explain myself bad...
    >>
    >> I wanted say if I can create anonymous class only for interface OR
    >> abstract class

    >
    > I do not understand what you mean, as well.
    >
    > What would be the third possibilty if not a class nor an interface?


    A non-abstract class [the PP said "OR /abstract/ class", my emphasis].

    --
    Yes, Virginia, there is a second Jena user conference: Palo Alto, Sep 2007.
    The shortcuts are all full of people using them.

    Hewlett-Packard Limited registered office: Cain Road, Bracknell,
    registered no: 690597 England Berks RG12 1HN
     
    Chris Dollin, Mar 26, 2007
    #5
  6. josh

    Oliver Wong Guest

    "josh" <> wrote in message
    news:...
    > On 26 Mar, 14:36, Tom Hawtin <> wrote:
    >> josh wrote:
    >>
    >> > Shape2D -> Interface
    >> > Shape3D -> abstract class

    >>
    >> > Can I do an anonymous class only with these two class-typo?

    >>
    >> You want an anonymous inner class that is a subtype of two types?

    >
    > sorry I explain myself bad...
    >
    > I wanted say if I can create anonymous class only for interface OR
    > abstract class


    Not sure what you mean, so I'm going to list a bunch of true
    statements in case one of them answers your question. These are all true
    statements:

    * You can create an anonymous class which implements a specified
    interface, e.g. "new Shape2D() {...};".
    * You can create an anonymous class which extends a specified class, e.g.
    "new Shape3D() {...};".
    * You can create an anonymous class which implements an interface and
    extends a class at the same time, using Tom Hawtin's trick.
    * You can't create an anonymous class which doesn't extend anything at
    all, since every object extends Object.

    - Oliver
     
    Oliver Wong, Mar 26, 2007
    #6
  7. josh

    Tom Hawtin Guest

    josh wrote:
    >
    > I wanted say if I can create anonymous class only for interface OR
    > abstract class


    You can extend a concrete (i.e. non-abstract) class, so long as it isn't
    final (and has an accessible constructor, etc.).

    If you don't want to extend or implement anything in particular, you can
    just extend Object. Not sure what you would do with that though. You
    could override toString to generate a String lazily, I guess. It could
    be used as a lock (the outer class and method name may appear in stack
    traces, but a local class is the usual way of doing that). For people
    who like writing hideously obscure code that few will understand the
    subtleties of, I believe it can be used to exploit final field semantics
    (anonymous means the class has no name, it still declares a type).

    Tom Hawtin
     
    Tom Hawtin, Mar 26, 2007
    #7
  8. josh

    josh Guest

    I post the code so I can explain better my doubts.

    when I make this:

    field.addActionListener(
    new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    // do something
    }
    }
    )
    then compiler when meets that expression evaluates like as:
    public class MyHandler implements ActionListener {}
    ActionListener is an interface

    window.addWindowListener
    (
    new WindowAdapeter()
    {
    public void windowClosing(WindowEvent e)
    {
    // do something
    }
    }
    )
    then compiler when meets that expression evaluates like as:
    public class MyHandler extends WindowAdapter() {}
    WindowAdapter is an abstract class

    so if I had neither an interface and neither an abstract class
    could I do it?

    myFunction(
    new MyClass()
    {

    }
    )

    Thanks
     
    josh, Mar 27, 2007
    #8
  9. Hi,

    josh wrote:
    > so if I had neither an interface and neither an abstract class
    > could I do it?
    >
    > myFunction(
    > new MyClass()
    > {
    >
    > }
    > )
    >
    > Thanks


    Yes you can do so, in case of MyClass being a welldefined (abstract or
    concrete) class or interface. Anonymous inner classes are not more than
    a shortcut for what you wrote above. (I think you understood that point?)

    What you write above is a shortcut for:

    class AnonymousSubclass extends MyClass {}
    ....
    myFunction(new AnonymousSubclass());
    ....

    Of course, - in this special case - this only works, if MyClass is *not*
    abstract.

    But the important question is: What does myFunction want to do with the
    given Object? What is the signature of myFunction?

    After all, I am still not sure if I understand your question correctly.

    Could you give a short but *complete*, compilable example with the
    definition of MyClass as well?

    Ciao,
    Ingo
     
    Ingo R. Homann, Mar 27, 2007
    #9
  10. Hi,

    josh wrote:
    > so if I had neither an interface and neither an abstract class
    > could I do it?
    >
    > myFunction(
    > new MyClass()
    > {
    >
    > }
    > )


    Ah, perhaps you mean: There is no class or interface MyClass at all?

    Then you could do this in a slightly different syntax:

    myFunction(new Object(){});

    You could even do the following:

    myFunction(new Object(){
    void foo() {
    System.out.println("Hello World!");
    }
    });

    The problem is: Of course, that only works, if the signature of
    "myFunction" is "Object". And if that is the case, the function "foo"
    can never be called, because the class Object itself has no method
    foo(). (You could use reflection, but that is another question.)

    So, the question remains the same: What is the signature of
    myFunction()? Or differently asked: Why do you want to call a function
    with an "undefined" (whatever that means) parameter?

    Ciao,
    Ingo
     
    Ingo R. Homann, Mar 27, 2007
    #10
  11. josh

    josh Guest

    > Yes you can do so, in case of MyClass being a welldefined (abstract or
    > concrete) class or interface. Anonymous inner classes are not more than
    > a shortcut for what you wrote above. (I think you understood that point?)


    yes but my MyClass is a simple concrete class so its shortcut cannot
    be
    class AnonymousSubclass extends MyClass {} because
    a superclass cannot extends itself

    >
    > Of course, - in this special case - this only works, if MyClass is *not*
    > abstract.


    I understood that these shortcuts are for abstract too...

    > Could you give a short but *complete*, compilable example with the
    > definition of MyClass as well?

    ok I will give you an example
    Bye
     
    josh, Mar 27, 2007
    #11
  12. Hi,

    josh wrote:
    >>Yes you can do so, in case of MyClass being a welldefined (abstract or
    >>concrete) class or interface. Anonymous inner classes are not more than
    >>a shortcut for what you wrote above. (I think you understood that point?)

    >
    > yes but my MyClass is a simple concrete class so its shortcut cannot
    > be class AnonymousSubclass extends MyClass {} because
    > a superclass cannot extends itself


    Why do you think that the AnonymousSubClass would must have itself as
    superclass? It would have MyClass as superclass! Just try it.

    Ciao,
    Ingo, still not recognizing your problem
     
    Ingo R. Homann, Mar 27, 2007
    #12
  13. josh

    Oliver Wong Guest

    "josh" <> wrote in message
    news:...
    >> Yes you can do so, in case of MyClass being a welldefined (abstract or
    >> concrete) class or interface. Anonymous inner classes are not more than
    >> a shortcut for what you wrote above. (I think you understood that
    >> point?)

    >
    > yes but my MyClass is a simple concrete class so its shortcut cannot
    > be
    > class AnonymousSubclass extends MyClass {} because
    > a superclass cannot extends itself


    In the code "class AnonymousSubclass extends MyClass {}", there is no
    sign of a superclass extending itself. All that that code indicates is
    that AnonymousSubclass extends MyClass. In particular, AnonymousSubclass
    is not extending itself: it is extending MyClass.

    - Oliver
     
    Oliver Wong, Mar 27, 2007
    #13
  14. josh

    josh Guest

    Now I post the code:

    I have an interface Shape
    I have a class Point implements Shape
    I have a class Circle extends Point

    ********************************************************************************
    (1)when I want to create an anonymous class that implements an
    interface I do:
    class java_test
    {
    public static void doShape(Shape s)
    {
    double v, a;

    v = s.volume();

    a = s.area();

    System.out.println("Volume: " + v + " Area: "+ a);
    }

    public static void main(String args[])
    {
    doShape(
    new Shape() // anonymous
    {
    public double area() {return 0.0;}
    public double volume() {return 0.0;}
    }
    );

    }
    here new Shape is like I was doing:
    class Anon implements Shape() {};
    Anon a = new Anon();
    doShape(a);
    *******************************************************************************

    ********************************************************************************
    (2)when I want to create an anonymous class that extends a superclass
    I do:

    class java_test
    {
    public static void doPoint(Point p)
    {
    System.out.println("Area: " + p.area());
    }

    public static void main(String args[])
    {
    doPoint(
    new Point() // anon
    {
    // HERE THE DOUBT ERROR
    public double radius;

    public double area()
    {
    return Math.PI * radius * radius;
    }

    }
    );
    }
    }
    here is like I was doing class Anon extends Point {} but here is my
    problem. When in the real
    code I do class Circle extends Point then in Circle I have a "radius"
    variable where I pass
    a value... and so when I call area() it returns a correct value. In
    the anonymous example
    I cannot do a correct area() overriding. Infact in Point area()
    returning 0.0 because a Point
    cannot have an area...AND THIS IS A BIG DOUBT
    ********************************************************************************
     
    josh, Mar 28, 2007
    #14
  15. josh

    Tom Hawtin Guest

    josh wrote:
    > new Point() // anon
    > {
    > // HERE THE DOUBT ERROR
    > public double radius;
    >
    > public double area()
    > {
    > return Math.PI * radius * radius;
    > }
    >
    > }


    > I cannot do a correct area() overriding. Infact in Point area()
    > returning 0.0 because a Point


    You never assign radius a value. It'll remain at the default (0.0). So
    your return expression of

    Math.PI * radius * radius

    will evaluate as

    3.14159... * 0.0 * 0.0

    Hence the value you see.

    Tom Hawtin
     
    Tom Hawtin, Mar 28, 2007
    #15
  16. Hi,

    in addition to what Tom said: Of course it is possible to use valid
    values for 'radius', e.g.:

    josh wrote:
    > class java_test
    > {
    > public static void doPoint(Point p)
    > {
    > System.out.println("Area: " + p.area());
    > }
    >
    > public static void main(String args[])
    > {


    final double radius=123.456;

    > doPoint(
    > new Point()
    > {
    > public double area()
    > {
    > return Math.PI * radius * radius;
    > }
    >
    > }
    > );
    > }
    > }


    Ciao,
    Ingo
     
    Ingo R. Homann, Mar 28, 2007
    #16
  17. josh

    Lew Guest

    Ingo R. Homann wrote:
    > Hi,
    >
    > in addition to what Tom said: Of course it is possible to use valid
    > values for 'radius', e.g.:
    >
    > josh wrote:
    >> class java_test
    >> {
    >> public static void doPoint(Point p)
    >> {
    >> System.out.println("Area: " + p.area());
    >> }
    >>
    >> public static void main(String args[])
    >> {

    >
    > final double radius=123.456;
    >
    >> doPoint(
    >> new Point()
    >> {
    >> public double area()
    >> {
    >> return Math.PI * radius * radius;
    >> }
    >>
    >> }
    >> );
    >> }
    >> }


    To elaborate: the anonymous class has access to "radius" from the enclosing
    class because inner classes have access to final variables in the enclosing
    context.

    -- Lew
     
    Lew, Mar 28, 2007
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,943
    Thomas Weidenfeller
    Oct 12, 2005
  2. Reporter
    Replies:
    3
    Views:
    496
    Mike Schilling
    May 12, 2007
  3. Trans
    Replies:
    2
    Views:
    320
    Trans
    Jan 29, 2005
  4. Trans
    Replies:
    2
    Views:
    187
    Trans
    Aug 30, 2005
  5. Replies:
    1
    Views:
    239
Loading...

Share This Page