Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > anonymous class

Reply
Thread Tools

anonymous class

 
 
josh
Guest
Posts: n/a
 
      03-26-2007
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() {}

 
Reply With Quote
 
 
 
 
Tom Hawtin
Guest
Posts: n/a
 
      03-26-2007
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
 
Reply With Quote
 
 
 
 
josh
Guest
Posts: n/a
 
      03-26-2007
On 26 Mar, 14:36, Tom Hawtin <use...@tackline.plus.com> 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




 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      03-26-2007
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

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      03-26-2007
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

 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      03-26-2007

"josh" <> wrote in message
news: ups.com...
> On 26 Mar, 14:36, Tom Hawtin <use...@tackline.plus.com> 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


 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      03-26-2007
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
 
Reply With Quote
 
josh
Guest
Posts: n/a
 
      03-27-2007
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





 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      03-27-2007
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

 
Reply With Quote
 
Ingo R. Homann
Guest
Posts: n/a
 
      03-27-2007
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this a local anonymous class or a member anonymous class Reporter Java 3 05-12-2007 05:23 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Attributation works in literal class but not in anonymous class? Trans Ruby 2 08-30-2005 07:26 PM
help with an anonymous array of anonymous hashes noeldamonmiller@gmail.com Perl Misc 1 02-10-2005 01:08 AM
[YAML] Cant dump anonymous class Class? Trans Ruby 2 01-29-2005 05:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57