3

I have the following method in a class:

public void Process(Measurable x)
{
    String y = x.getResult();

    for (int k = 0; k<item.length; k++)
    {
        if (y.equals(item[k])) tally[k]++;
    }
}

My question is, how do I call the Process method? Calling it with

Process(Measurable y);

in the default constructor or the driver class doesn't work, nor does calling it with no parameter (as I would expect).

  • 2
    Provide an implementation instance of the `Measurable` interface. You can even use an [anonymous class](http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm) to handle the job. – Luiggi Mendoza Mar 14 '13 at 16:27
  • What driver class are you talking about? does this method belong to the class `Process`? – asgs Mar 14 '13 at 16:27
  • 1
    Well what argument do you *want* to use? Do you understand that you're trying to supply information to the method? Where do you expect that information to come from? – Jon Skeet Mar 14 '13 at 16:30

6 Answers6

2
// How to call your Process method
Measureable y = new ConcreteMeasureable()
Process(y);

// Assuming you have something like this... 
class ConcreteMeasureable implements Measureable
{
    @Override
    public String getResult()
    {
        return "something here";
    }
}
bn.
  • 7,739
  • 7
  • 39
  • 54
1

Call the Process method with an instance of a class that implements the Measurable interface; even an anonymous class would do.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1

You would call it with a class implementing the Measurable interface. For example:

Assume you have the class:

class Foo implements Measurable {
    @Overrride
    public String getResult(){ return "bar"; }
}

And then you could call Process like so:

Process(new Foo());

This takes advantage of an Object Oriented Programming idea called Polymorphism.

People have also mentioned that you can use anonymous inner class like so:

Process(new Measurable(){
    @Override
    public String getResult() {
        return "hey!";
    }
});
Community
  • 1
  • 1
jjnguy
  • 136,852
  • 53
  • 295
  • 323
1

You need to create an object that implements Measurable:

public class Test {
  public static void main(String args[]) {
    new Test().test();
  }

  public interface Measurable {
    String getResult();
  }

  // Here to make `Process` compile.
  String[] item;
  int[] tally;

  public void Process(Measurable x) {
    String y = x.getResult();

    for (int k = 0; k < item.length; k++) {
      if (y.equals(item[k])) {
        tally[k]++;
      }
    }
  }

  static class M implements Measurable {

    @Override
    public String getResult() {
      return "M";
    }

  }

  public void test() {
    // Create an Object on the fly that implements Measurable (in a rather silly way).
    Process(new Measurable() {

      @Override
      public String getResult() {
        return "Hello";
      }

    });
    // Do it a bit more normally.
    Process(new M());
  }
}
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

You can treat objects that implement the interface as the interface type using polymorphism, even though you can't directly instantiate an object of it.

For example, if Measurable is an interface, and we have a class like this:

public class MeasuredObject implements Measurable {
    ...
}

Then we can do something like the following:

MeasuredObject object = new MeasuredObject();
Measurable measurableObject = (Measurable) object;

This should show that calling your method is possible.

Process(measurableObject);

While the explicit type casting isn't necessary, it should help illustrate the concept.

(Small side note: consider adhering to Java coding standards and making your method names into camelCase. It makes your code much more readable.)

asteri
  • 11,402
  • 13
  • 60
  • 84
0

Try something like:

public class Driver {
    public static void main(String...args) {
        Driver driver = new Driver();
        Foo foo = new Foo();
        driver.process(foo);
    }

    public void process(Measurable x) {
        String y = x.getResult();

        for (int k = 0; k<item.length; k++) {
            if (y.equals(item[k])) tally[k]++;
        }
    }

    private class Foo implements Measurable {
        String getResult() {
            /* Do stuff */
        }
    }

    public interface Measurable {
        String getResult();
    }
}

There's also a y, item, and tally in there somewhere.

P.S. In Java method names are generally lower camel cased.

Alex
  • 349
  • 1
  • 10