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.
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
// | |
1 |
// | |
1 |
# | |
1 |
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.
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.
// | |
1 |
and the jythonClass.py modified above to extend both classes,
# | |
1 |
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
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.
# | |
1 |
and now the jythonClass tries to inherit javaClassA implemented in java and javaClassB implemented in jython.
# | |
1 |
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
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.
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.




2 comments:
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.
Hi Shwick,
Thanks a lot for pointing out, its now corrected.
Cheers,
S.Prasanna
Post a Comment