Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
Mar 20, 1997

Passing a Method to Another Class to be Called By That Class

Question:
Is it possible to pass a method to another class, so that method can be called by that class at a later date? For example, in C you can pass an event handler to a function, and that handler can be later called by that function if a specific event occurs.

Answer:
This issue comes up frequently with object-oriented languages and fortunately Java provides a very clean solution for accomplishing this: Interfaces.

An interface is basically a collection of method definitions without the implementations. A class is said to implement an interface when it provides implementations for the methods listed in that interface.

For example, the Runnable interface in Java looks like this:

public interface java.lang.Runnable
       {
               public abstract void run();
       }
It specifies a single method called 'run' that doesn't take any arguments and doesn't return anything. It's just a specification at this point -- no body has been defined for the method.

When a class implements the Runnable interface, it has to provide the implementation for the run() method as it sees fit. For example:

       public class Foo implements Runnable {

               public void run() {
                       // Foo's implementation of run() goes here
               }
       }
It just so happens that the Thread class can be initialized using an object that implements the Runnable interface. Because class Foo implements the Runnable interface, you can create a Thread in this manner:
Thread t = new Thread(new
Foo());
 
Now the Thread class is guaranteed to be able to call the run() method of Foo.

Let's see how this mechanism can solve your problem. Let's say you have a class Chef and a class Food. You want class Chef to be able to call the cook() method in class Food, and you somehow want to 'pass' the cook() method in Food to the Chef. Here's how to accomplish this:

       // define an interface called cookable that
       // specifies the cook() method
       public interface Cookable {
               public abstract void cook();
       }


       // Let class Food implement that interface
       // by providing the body for the cook() method
       public class Food implements Cookable {

               public void cook() {
                       // get cooked
               }
       }

       // Let the Chef call cook() on the Cookable object it gets
       // All the Chef knows is that the object is Cookable.
       public class Chef {

               public makeDish(Cookable dish) {
                       dish.cook();
               }

       }

Now, if you had a Kitchen class, you could easily have the Chef cook some food:
       Chef chefWang = new Chef();

       Food cashewChicken = new Food();
       chefWang.makeDish(cashewChicken);

       Food noodles = new Food();
       chefWang.makeDish(noodles);

Since noodles and cashewChicken both implement the Cookable interface, chefWang can just call their cook() methods and the dishes will be ready in no time.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date