Re: Function pointers (Callback functions) in Java ?

From: Andrew McDonagh (news_at_andrewcdonagh.f2s.com)
Date: 02/28/05


Date: Mon, 28 Feb 2005 19:28:56 +0000

knightowl wrote:
>>class MyCallbackUsingClass {
>>
>> public MyCallbackUsingClass() {
>> Oops callback = new OopsImplemenation();
>> }
>>

see comment below (and note I made the mistake of assigning the new
object to a local var called 'callback'. I meant to assign it to the
member var called 'callback'

so code above should have been:

>>class MyCallbackUsingClass {
>>
>> public MyCallbackUsingClass() {
>> callback = new OopsImplemenation();
>> }
>>

>>
>> public doSomething() {
>> callBack.update(someStuff);
>> }
>>
>> private Oops callback;
>>
>>
>> class OopsImplementation implements Oops {
>>
>> public update(SomeStuff stuff) {
>> // perform update
>> }
>>
>> }
>>}
>
>
> I am new the Java, and I am trying to get a handle on the above
> callback example. I wrote a parsing engine that used call back
> functions, and I used reflection (a sackable offense as I am now aware
> of it).

;-)

>
> In the above I don't see the Opps interface. I guess i am not not
> making the connection
>

The code was a very simple example of creating a named inner class
implementation of an interface, rather than simply creating an anonymous
inner class.

The MyCallbackUsingClass constructor is creating an instance of
OopsImplementation and assigning it to its member variable 'callback'

The Type of the member var 'callback' is Oops

The actual interface code was in the post I replied too - I simply
didn't repeat it. Sorry if this caused any confusion.

Its may be worth knowing that you can create anonymous inner classes
derived from other classes too, they are solely for implementing Interfaces.

For example, you have a swing control that generates mouse events and
you want to have some code executed when the mouseEntered event is
generated.

You have a few choices.

You could:

1) create an anonymous inner class that implements the entire
MouseListener interface. However, as you are only interested in
mouseEntered() events, you'd be implementing lots of methods that do
nothing.

2) create an anonymous inner class that derives from the Abstract class
MouseAdapter which have all of the mouseListener interface methods
defined and you just need to over ride the one(s) you want - see
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/MouseAdapter.html

3) do either 1 or 2 above but using normal top level classes instead of
anonymous inner classes - works just the same.

HTH

Andrew



Relevant Pages

  • Re: abstract class versus interface
    ... >> This is an anonymous inner class that implements the Runnable interface. ... this is similar to a named inner class that implements the ...
    (comp.lang.java.programmer)
  • Re: abstract class versus interface
    ... > No it can't - an interface can never be instantiated. ... > This is an anonymous inner class that implements the Runnable interface. ... which has nothing to do with "instantiating an ... > Anonymous inner classes are not restricted to being used with interfaces. ...
    (comp.lang.java.programmer)
  • Re: Problem with imported callback to C#
    ... Usually it happens when callback declaration is using non-standard ... exposes interface, which enables to set a callback, which is called ... DirectShow graph with mentioned face detector filter and calls its ... int BufferCB; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: we have a "this" keyword, but what if... we also had "component" keyword??
    ... We can easily mimic this behavior in functional languages by ... > OO languages do this automatically, for every instance method the ... > interface and a protected interface to its decendants. ... > class instance to the inner class, however what is not provided is the ...
    (comp.object)
  • Re: Question about calling constructors of inner classes across different packages
    ... that implements the interface, and a second class that extends the ... first class (and the inner class as well, ... class AI (extends interface I), ... Again, note the different packages, as that's what leads to the issue ...
    (comp.lang.java.programmer)