anonymous class

J

josh

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() {}
 
T

Tom Hawtin

josh said:
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
 
J

josh

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

Ingo R. Homann

Hi josh,
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
 
C

Chris Dollin

Ingo said:
Hi josh,


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
 
O

Oliver Wong

josh said:
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
 
T

Tom Hawtin

josh said:
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
 
J

josh

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
 
I

Ingo R. Homann

Hi,
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
 
I

Ingo R. Homann

Hi,
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
 
J

josh

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
 
I

Ingo R. Homann

Hi,
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
 
O

Oliver Wong

josh said:
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
 
J

josh

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
********************************************************************************
 
T

Tom Hawtin

josh said:
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
 
I

Ingo R. Homann

Hi,

in addition to what Tom said: Of course it is possible to use valid
values for 'radius', e.g.:
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
 
L

Lew

Ingo said:
Hi,

in addition to what Tom said: Of course it is possible to use valid
values for 'radius', e.g.:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top