1 // This file illustrates the cross language polymorphism using directors.
4 // CEO class, which overrides Employee::getPosition().
6 class CEO extends Manager {
7 public CEO(String name) {
10 public String getPosition() {
13 // Public method to stop the SWIG proxy base class from thinking it owns the underlying C++ memory.
14 public void disownMemory() {
23 System.loadLibrary("example");
24 } catch (UnsatisfiedLinkError e) {
25 System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e);
30 public static void main(String argv[])
33 // Create an instance of CEO, a class derived from the Java proxy of the
34 // underlying C++ class. The calls to getName() and getPosition() are standard,
35 // the call to getTitle() uses the director wrappers to call CEO.getPosition().
37 CEO e = new CEO("Alice");
38 System.out.println( e.getName() + " is a " + e.getPosition() );
39 System.out.println( "Just call her \"" + e.getTitle() + "\"" );
40 System.out.println( "----------------------" );
43 // Create a new EmployeeList instance. This class does not have a C++
44 // director wrapper, but can be used freely with other classes that do.
46 EmployeeList list = new EmployeeList();
48 // EmployeeList owns its items, so we must surrender ownership of objects we add.
51 System.out.println( "----------------------" );
53 // Now we access the first four items in list (three are C++ objects that
54 // EmployeeList's constructor adds, the last is our CEO). The virtual
55 // methods of all these instances are treated the same. For items 0, 1, and
56 // 2, all methods resolve in C++. For item 3, our CEO, getTitle calls
57 // getPosition which resolves in Java. The call to getPosition is
58 // slightly different, however, because of the overidden getPosition() call, since
59 // now the object reference has been "laundered" by passing through
60 // EmployeeList as an Employee*. Previously, Java resolved the call
61 // immediately in CEO, but now Java thinks the object is an instance of
62 // class Employee. So the call passes through the
63 // Employee proxy class and on to the C wrappers and C++ director,
64 // eventually ending up back at the Java CEO implementation of getPosition().
65 // The call to getTitle() for item 3 runs the C++ Employee::getTitle()
66 // method, which in turn calls getPosition(). This virtual method call
67 // passes down through the C++ director class to the Java implementation
68 // in CEO. All this routing takes place transparently.
70 System.out.println( "(position, title) for items 0-3:" );
72 System.out.println( " " + list.get_item(0).getPosition() + ", \"" + list.get_item(0).getTitle() + "\"" );
73 System.out.println( " " + list.get_item(1).getPosition() + ", \"" + list.get_item(1).getTitle() + "\"" );
74 System.out.println( " " + list.get_item(2).getPosition() + ", \"" + list.get_item(2).getTitle() + "\"" );
75 System.out.println( " " + list.get_item(3).getPosition() + ", \"" + list.get_item(3).getTitle() + "\"" );
76 System.out.println( "----------------------" );
78 // Time to delete the EmployeeList, which will delete all the Employee*
79 // items it contains. The last item is our CEO, which gets destroyed as well.
81 System.out.println( "----------------------" );
85 System.out.println( "java exit" );