Solved

Can Java handle void *functionTypes as C++ does

Posted on 1997-07-16
2
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

623 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