Click to See Complete Forum and Search --> : User Supplied methods in Classes


Brad Cadle
January 23rd, 1999, 06:24 PM
Hi All,

I am a Java Beginner. I am trying to code up some numerical routines which expect to call some user supplied subroutines. What is the most appropriate way to do this in java. In fortan or C one would could declare an external function which the user would provide. Also in C one supply this by passing a pointer to a function. In C++ one could declare a virtual function of a class and derive a new class from it.


I am having a single class that will contain all the numerical methods, but some of the methods will be calling user supplied funtions.


1) One way could be to have the user create an object of some second class that is passed to the numerical method class which then owns it. The numerical methods class could then call the function supplied into the second class passed to it.


Are there alternate ways in Java to handle having my class call the third party supplied functions? What are they?


-Brad

brad Cadle
January 23rd, 1999, 06:24 PM
Hi All,

I am a Java Beginner. I am trying to code up some numerical routines which expect to call some user supplied subroutines. What is the most appropriate way to do this in java. In fortan or C one would could declare an external function which the user would provide. Also in C one supply this by passing a pointer to a function. In C++ one could declare a virtual function of a class and derive a new class from it.


I am having a single class that will contain all the numerical methods, but some of the methods will be calling user supplied funtions.


1) One way could be to have the user create an object of some second class that is passed to the numerical method class which then owns it. The numerical methods class could then call the function supplied into the second class passed to it.


Are there alternate ways in Java to handle having my class call the third party supplied functions? What are they?


-Brad

Dev K
January 26th, 1999, 11:21 AM
Hi,


One way is using reflection mechanism of java 1.1. But you should know the

class name of class supplied by user. But user Class should also be java class.

Reflection mechanism is vast and is not quiet conceivable to discuss here.

Core Java 1.1 Volume-I from sunsoft press, author Cay S. Horstmann and

Gary C. will of some help.

Hope this will help.

Zafir Anjum
January 27th, 1999, 12:08 AM
You would need to use 'interface'. In Java there is no multiple inheritance and

no function pointer. Interfaces circumvent the need of MI and also help out where

we need normally use function pointers in C or C++.


Define an interface with one (or more) functions. When calling your numeric

function, you'd have to supply an object that implements this interface.