Subclassing Java Classes in Jython
A Short Example
The example below should both demonstrate how this subclassing is
performed and why it is useful. At first glance, the code looks
exactly like subclassing any other Python class. The key difference
in this example is that awt.event.ActionListener is a Java class, not
a Python one. In the 4th line from the end,
a Java method is being called that
requires an instance of the Java class ActionListener. By providing a
Python subclass of this Java class, everybody is happy.
Note: This example can be accomplished much more elegantly by using
(and event properties).
from java import awt
if event.getActionCommand() == "Spam":
print 'Spam and eggs!'
f = awt.Frame("Subclassing Example")
b = awt.Button("Spam")
Calling Methods in Your Superclass
In Python, if I want to call the foo method in my superclass, I use
This works with the majority of methods, but protected methods cannot be
called from subclasses in this way.
Instead you have to use the "self.super__foo()" call style.
The following example shows how the java.io.InputStream class can be
effectively subclassed. What makes this class difficult is that the
read method is overloaded for three different method signatures:
The first one of these methods must be overridden in a subclass.
The other two versions can be ignored. Unfortunately, Python has
no notion of method overloading based on type signatures (this might
be related to the fact that Python doesn't have type signatures
;-) In order to implement a subclass of java.io.InputStream that
overrides the "read" method, a Python method must be implemented that
handles all three possible cases. The example below shows the easiest
way to acheive this:
- abstract int read()
- int read(byte)
- int read(byte, int, int)
from java.io import InputStream
def read(self, *args):
if len(args) > 0:
# int read(byte)
# int read(byte, int, int)
return apply(InputStream.read, (self,)+args)
io = InfiniteOnes()
for i in range(10):
To continue the example above, this new instance of
java.io.InputStream can be passed to any Java method that expects an
InputStream as shown below:
from java.io import DataInputStream
dp = DataInputStream(io)
Invoking Your Superclass's Constructor
You can explictly invoke your superclass's constructor using the
standard Python syntax of explictly calling the "__init__" method on
the superclass and passing in "self" as the first argument. If you
wish to call your superclass's constructor, you must do so within your
own "__init__" method. When your "__init__" method finishes, if your
Java superclasses have not yet been explicitly initialized, their
empty constructors will be called at this point.
It's important to realize that your superclass is not initialized
until you either explictly call it's "__init__" method, or your own
"__init__" method terminates. You must do one of these two things
before accessing any methods in your superclass.
This example shows how the superclass's constructor can be effectively
called in order to explictly choose a non-empty version.
from java.util import Random
def __init__(self, multiplier=1.0, seed=None):
self.multiplier = multiplier
if seed is None:
return Random.nextDouble(self) * self.multiplier
r = rand(100, 23)
for i in range(10):