Solved

Can Java handle void *functionTypes as C++ does

Posted on 1997-07-16
2
209 Views
Last Modified: 2008-03-06
I am writing on a data structure code library in Java and run into the need of function types. Here is a code example in C++ ( in a binary search tree class)

this would be in the BStree.h Header file in C++

...
...

typedef void (*functionType)(Object & anItem);
// This function will be defined outside of this class

class BSTree
{
      ...
      ...

public:
      inOrderTraverse(functionType visit)
      preOrderTraverse(functionType visit)
      etc.
      etc.

} // end class


No I am wondering how to use the same mechanism in Java in order to write the same kind of function.

P.S.
As a possability I am able to transform the whole thing into an Enumeration but that doesn't seem to be a nice solution.

Thanks a lot !!
0
Comment
Question by:meggenb
2 Comments
 
LVL 2

Expert Comment

by:webster030697
ID: 1223114
This doesn't answer your question exactly, but have a look at:

http://www.cerf.net/~paulp/java/btree/ 

or:

http://www.scism.sbu.ac.uk/jfl/jflcontents.html#chap15.html




0
 
LVL 1

Accepted Solution

by:
mann061997 earned 100 total points
ID: 1223115
This is the perfect application for interfaces. Define an interface containing the required function prototypes. These too can be generalized by using superclasses or interfaces.

Then, instead of passing a function pointer, pass a reference to an object implementing the interface (the interface can be used just like a type).
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
backtracking recursion  code 19 58
split string containing \r\n in Java 46 43
more than one jdk and one jre 1 41
how to install java on RHEL image on EC2 4 27
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question