Implementing Java Interfaces in Jython

Saturday, September 27, 2008

Implementing Java Interfaces is a common task where different java classes implement the interface methods according to their design behavior, however I was curious to learn how to implement java interfaces from jython, though you cannot vaguely call that a certain jython class implements a java interface because of the rules to which interfaces are bound to in java, one can say that a jython class implementing a java interface is something similar to a jython class extending a java class whose methods are not defined in java and the jython class need not necessarily implement all methods which the java interface defines because of the nature of which jython script works as we will see below.

An Interface in java is a class which declares certain methods which SHOULD be DEFINED by all implementing java classes, but when you implement a java inferface from jython, it doesn't serve much purpose as it merely means defines a similar method in jython class which is declared in java interface without adhering to any rules which apply to java interfaces, therefore the only application which I may think of using java interfaces from jython is in cases where one needs to access some constants defined in the java interface or defining similar methods of the java interface for which an equivalent implementation class was not provided in java. The below example will make things clear.



//
// JavaInterface.java
// Author: S.Prasanna
// A Simple java interface
//

public interface JavaInterface {

Object add(Object a, Object b);

void multiply(Object a, Object b);
}
1
2
3
4
5
6
7
8
9
10
11
12

Listing 2: JythonImpl.py


#
# Implementing Java Interface from jython
# JythonImpl.py
# Author: S.Prasanna
#

import JavaInterface
import traceback

class JythonImplA(JavaInterface):
""" Class JythonImplA implementing JavaInterface """

def __init__ (self):
print "This is JythonImplA class which implements a java interface"

def add(self, a, b):
return a + b

class JythonImplB(JavaInterface):
""" Class JythonImplB implementing JavaInterface """

def __init__ (self):
print "This is JythonImplB class which implements a java interface"

def add(self, a, b):
return a + b

def multiply(self, a, b):
return a * b

if __name__ == "__main__":

jythonClassA = JythonImplA()
print "JythonClassA add method output 10 + 20 =", jythonClassA.add(10,20)

try:
jythonClassA.multiply(10, 20)
except:
print
traceback.print_exc()

try:
jythonClassA.multiply(10, 20, 30, 40)
except:
print
traceback.print_exc()

try:
jythonClassA.divide(20, 10)
except:
print
traceback.print_exc()

print
jythonClassB = JythonImplB()
print "\nJythonClassB add method output =", jythonClassB.add(10, 20)
print "JythonClassB multiply method output 10 * 20 =", jythonClassB.multiply(10, 20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

Output:

C:\>jython JythonImpl.py
This is JythonImplA class which implements a java interface
JythonClassA add method output 10 + 20 = 30

Traceback (most recent call last):
File "JythonImpl.py", line 37, in ?
jythonClassA.multiply(10, 20)
AttributeError: abstract method "multiply" not implemented

Traceback (most recent call last):
File "JythonImpl.py", line 43, in ?
jythonClassA.multiply(10, 20, 30, 40)
TypeError: multiply(): expected 2 args; got 4

Traceback (most recent call last):
File "JythonImpl.py", line 49, in ?
jythonClassA.divide(20, 10)
AttributeError: divide

This is JythonImplB class which implements a java interface

JythonClassB add method output = 30
JythonClassB multiply method output 10 * 20 = 200

Explanation:

The java interface class has two methods which defines two methods add and multiply which takes two java Objects and returns a java Object. Note that the return type of multiply method is void or in other words, the multiply method doesn't return any value.

The jython script JythonImpl has two classes JythonImplA and JythonImplB which implement the java interface, note that the syntax for both inheritng and implementing a class in jython are the same as shown below.

class JythonImplA (JavaInterface):
class JythonImplB (JavaInterface):

Lines 9 - 17 Here the JythonImplA class defines only the add method, which is declared in the JavaInterface, though it should not be called as defining a java interface method, but its an add method which the JythonImpl overrides.

Lines 18 - 29 Here the JythonImplB class defines two methods add and multiply, which are declared in the JavaInterface, again these methods are jython class methods and not necessarily an implementation of the java inteface methods, see the return type of the multiply method in JythonImplB, it returns the product of two integers, whereas the java interface method defines void as the return type.

Lines 32 - 34 Here we instantiate JythonImplA and call its add method, though it did not define multiply method, its still perfectly legal as the method it defines has nothing to do with complying with java interface rules.

Lines 35 - 40 Here we try to call the multiply method of JavaImplA class which it doesn't define, note that since in this case there is no implementation of multiply method in JythonImplA, therefore it tries to call the SuperClass method, since the superclass JavInterface doesn't provide any implementation of multiply method, you will get an exception

AttributeError: abstract method "multiply" not implemented

Similarly when multiply is called with four parameters as in line 42, you will get an exception

TypeError: multiply(): expected 2 args; got 4

Because the multiply method in the superclass takes only two methods (but not implemented).

Lines 47 - 51 Here we try to call a non existent method divide and since even the superclass doesn't declare such a method, we will get an attribute error.

Lines 53 - 57 Here the JythonImplB class defines both the methods add and multiply which the superclass also declares, therefore you will get the expected output (note that the superclass method signature for the multiply method is different from what jython defines which returns the product of the two numbers).

In a nutshell when you say a Jython Class Implementing a Java Interface its ....

1. The Jython class simply extends a java class which has no method definitions (or the super class has only method declarations and defines constants)

2. As obvious, the jython class need not define all methods declared in the java class, but when it defines, its a jython class method which need not have the return types and arguments to match that of the java interface, its more or less a method overriding in jython.

3. When a jython class instance tries to access a method which is not defined in the jython class but declared in the super class (the java interface), you will get an error AttributeError: abstract method "methodname" not implemented since the java interface doesn't have any implementation of that method.

The only application where you need to implement a java interface from jython is when there is no implementing class in java or you need to access some constants defined in the corresponding java interface.

No comments:


Copyright © 2016 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.