Java Forum / General / March 2007
anonymous class
josh - 26 Mar 2007 12:57 GMT 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() {}
Tom Hawtin - 26 Mar 2007 13:36 GMT > 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
josh - 26 Mar 2007 15:47 GMT > > 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
Ingo R. Homann - 26 Mar 2007 15:52 GMT Hi josh,
>>>Shape2D -> Interface >>>Shape3D -> abstract class [quoted text clipped - 7 lines] > 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
Chris Dollin - 26 Mar 2007 15:55 GMT > Hi josh, > [quoted text clipped - 13 lines] > > 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].
 Signature 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
Oliver Wong - 26 Mar 2007 16:01 GMT >> > Shape2D -> Interface >> > Shape3D -> abstract class [quoted text clipped - 7 lines] > 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
Tom Hawtin - 26 Mar 2007 16:56 GMT > 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
josh - 27 Mar 2007 08:25 GMT 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
Ingo R. Homann - 27 Mar 2007 09:01 GMT Hi,
> so if I had neither an interface and neither an abstract class > could I do it? [quoted text clipped - 7 lines] > > 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
josh - 27 Mar 2007 09:32 GMT > 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
Ingo R. Homann - 27 Mar 2007 09:39 GMT Hi,
>>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 [quoted text clipped - 3 lines] > 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
josh - 28 Mar 2007 09:27 GMT 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 ********************************************************************************
Tom Hawtin - 28 Mar 2007 10:05 GMT > new Point() // anon > { [quoted text clipped - 7 lines] > > }
> 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
Ingo R. Homann - 28 Mar 2007 10:08 GMT Hi,
in addition to what Tom said: Of course it is possible to use valid values for 'radius', e.g.:
> class java_test > { [quoted text clipped - 5 lines] > public static void main(String args[]) > { final double radius=123.456;
> doPoint( > new Point() [quoted text clipped - 8 lines] > } > } Ciao, Ingo
Lew - 28 Mar 2007 12:58 GMT > Hi, > [quoted text clipped - 25 lines] >> } >> } 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
Oliver Wong - 27 Mar 2007 15:50 GMT >> 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 [quoted text clipped - 5 lines] > 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
Ingo R. Homann - 27 Mar 2007 09:07 GMT Hi,
> so if I had neither an interface and neither an abstract class > could I do it? [quoted text clipped - 5 lines] > } > ) 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
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|