Solved

Can Java handle void *functionTypes as C++ does

Posted on 1997-07-16
2
207 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now