Jython Inheritance: Extending Java classes from Jython

Monday, October 6, 2008

Today I was experimenting inheritance concepts in jython and java i.e extending a java class from a jython script. In my previous writing on jython, we explored how interface concepts works from jython to java, now we we will see how to inherit a java class from jython. Before diving in, let me refresh some fundamental concepts of the about Python and Java.

1. Python Supports multiple inheritance, i.e a python class can extend or inherit multiple python classes.
2. Java doesn't support multiple inheritance, one prime reason is to avoid "Diamond of Death".

Given this behavior, I was wondering how inheritance from jython to java would work, as obvious the design philosophy of jython should not violate fundamental OOPS design principles of java and jython respects that too as I learnt it the hard way trying to experiment the diamond to death problem.

Ok, we will first see how basic inheritance works from jython to java and multiple inheritance from jython to java (Curious to see what error it throws..)

In the below listing, we have three classes, BaseClass, javaClassA which extends BaseClass and jythonClass which extends javaClassA and see how inheritance works from jython to java.

Listing 1: BaseClass.java


//
// BaseClass.java
// Author: S.Prasanna
//

public class BaseClass {

public BaseClass() {}

public void baseClassMethod() {
System.out.println("I am the baseClassMethod of BaseClass");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13

Listing 2: javaClassA.java


//
// javaClassA.java
// Author: S.Prasanna
// javaClassA extends BaseClass
//

public class javaClassA extends BaseClass {

public javaClassA() {}

public void methodA() {
System.out.println("I am method A of javaClassA");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Listing 3: jythonClass.py


#
# Inheriting java classes from jython
# jythonClass.py
# Author: S.Prasanna
#

import javaClassA

class jythonClass(javaClassA):

def __init__(self):
print "This is jythonClass inheriting a java class"

if __name__ == "__main__":

jythonInstance = jythonClass()
jythonInstance.methodA()
jythonInstance.baseClassMethod()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Now if you compile the jython code, you will get the below output.

C:\>jython jythonClass.py
This is jythonClass inheriting a java class
I am method A of javaClassA
I am the baseClassMethod of BaseClass

The explanation is simple, the jythonClass extends javaClassA which inturn extends BaseClass and the jython instance calls the methods delegated to the inherited classes.

Now I tried to create a diamond to death scenario with another class javaClassB which also extends BaseClass and the jython class extends both javaClassA and javaClassB and tries to call the BaseClass method, as obvious the interpreter/compiler, whatever you call slaps with the error as you will see below.

Listing 4: javaClassB.java


//
// javaClassB.java
// Author: S.Prasanna
// javaClassB extends BaseClass
//

public class javaClassB extends BaseClass {

public javaClassB() {
}

public void methodB() {
System.out.println("I am method B of javaClassB");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

and the jythonClass.py modified above to extend both classes,


#
# Inheriting java classes from jython
# jythonClass.py
# Author: S.Prasanna
#

import javaClassA
import javaClassB

class jythonClass(javaClassA, javaClassB):

def __init__(self):
print "This is jythonClass inheriting a java class"

if __name__ == "__main__":

jythonInstance = jythonClass()
jythonInstance.methodA()
jythonInstance.methodB()
jythonInstance.baseClassMethod()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

The compiler is smart and throws the error message.

C:\Prasanna\myblogs\prasannatech.net\entries\jython_inheritance>jython jythonClass.py
Traceback (innermost last):
File "jythonClass.py", line 10, in ?
TypeError: no multiple inheritance for Java classes: javaClassB and javaClassA

The lesson learned is even though python supports multiple inheritance, once it comes to JVM, it should not violate any fundamental java principles. The last experiment I did is to write the javaClassB.java in jython, again the same error, but some more useful things can be inferred from the below code.

Listing 5: javaClassB.py


#
# javaClassB.py
# Author: S.Prasanna
#

import BaseClass

class javaClassB(BaseClass):

def __init__(self):
pass

def methodB():
print "I am method B of javaClassB"
1
2
3
4
5
6
7
8
9
10
11
12
13
14

and now the jythonClass tries to inherit javaClassA implemented in java and javaClassB implemented in jython.


#
# Inheriting java classes from jython
# jythonClass.py
# Author: S.Prasanna
#

import javaClassA
import javaClassB

class jythonClass(javaClassA, javaClassB.javaClassB):

def __init__(self):
print "This is jythonClass inheriting a java class"

if __name__ == "__main__":

jythonInstance = jythonClass()
jythonInstance.methodA()
jythonInstance.methodB()
jythonInstance.baseClassMethod()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

The error message is little different here, but points to the same reason

C:\>jython jythonClass.py
Traceback (innermost last):
File "jythonClass.py", line 10, in ?
TypeError: no multiple inheritance for Java classes: org.python.proxies.javaClassB$javaClassB$0 and javaClassA

One more thing to note is that when you have two java classes with the same name with one implemented in java and the equivalent one in jython, any jython code which imports the java class will import the equivalent jython class module.

Enough of inheritance, the moral: when a scripting language is implemented in a different platform you need to live with that platform's design rules and jython developers had done a great job in this to impress both python as well as java users on a case by case basis.

3 comments:

shwick said...

Great blog, explained inheritance very clearly from Java to Jython.

Minor error in Listing 1: BaseClass.java, shows javaClassA.java instead of BaseClass.java. The link to the class still works.

Thanks.

Prasanna Seshadri said...

Hi Shwick,

Thanks a lot for pointing out, its now corrected.

Cheers,
S.Prasanna

Ronen Agranat said...

Thanks for the interesting article.


Copyright © 2008 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.