3.3 Object Passing through Interface

(See horb/examples/interfaceSend for example code.)

Java allows the use of an interface as a type. Consider the following interface and class.

        // Data.java
        package horb.examples.interfaceSend;

        import horb.orb.*;
        import java.io.*;

        public interface Data {
          public void work_Local();
          public void list_Local(PrintStream ps);
        }

        // Server.java
        package horb.examples.interfaceSend;

        public class Server {

          Data work(Data data) {
            data.work_Local();
            return data;
          }
        }

An object implementing interface Data can be passed to Server.work() as an argument. HORB treats such feature correctly when Server.work() is remotely called. Let's assume two classes, 1) TwoD, a two dimensional array, and 2) Triangle, a triangular array that are implementation classes of interface Data. Here is a list of Triangle.

        // Triangle.java
        package horb.examples.interfaceSend;
        import java.io.*;

        /** triangular array */
        public class Triangle implements Data {
          public double data[][];

          Triangle() {}                          // this is required.

          Triangle(int n) {
            int x = 0;
            data = new double[n][];
            for (int i = 0; i < n; i++) {   // initialize array
              data[i] = new double[i];
              for (int j = 0; j < i; j++)
                data[i][j] = x++;
            }
          }

          public void work_Local() {
            for (int i = 0; i < data.length; i++)
              for (int j = 0; j < data[i].length; j++)
                  data[i][j] *= 2;             // repeat gag
          }

          public void list_Local(PrintStream ps) { . . .}
        }

TwoD and Triangle can be treated as the same type in remote method call.

        . . .
        Server_Proxy server = new Server_Proxy(HorbURL(somewhere));
        Data data1 = new TwoD(8);
        server.work(data1);

        Data data2 = new Triangle(8);
        server.work(data2);

Data must be compiled with the HORBC compiler to generate Data_Proxy and Data_Skeleton. (Try examples in examples/interfaceSend.)

Here is another example:

        class Param {
            public Data data;
        }

When an instance of this class is passed, HORB transfers the instance variable data as an instance of an implementation class of interface Data.