Problems extending an abstract Java class in Jython.

Ged gedb at rushcoding.co.uk
Fri Jan 3 06:48:42 EST 2003


I've refined the problem a little.  I now have a Java class and a
Jython script.

First of all is my Abstract Java class.

-----------------------------------------------------------
01 public abstract class Abstract {
02	
03    public Abstract() {
04       System.out.println("Java Class Abstract Constructing");
05       // showInt(5);
06       System.out.println("Java Class Abstract Constructed");
07    }
08	
09    public abstract void showInt(int i);
10		
11    public void showSix() {
12       showInt(6);
13    }
10 }
--------------------------------------------- Abstract.java

My Jython script looks like this.
-----------------------------------------------------------
01 import Abstract
02
03 class extends(Abstract):
04
05    def __init__(self):
06       print "Jython class 'extends' initialised"
07
08    def showInt(self, i):
09       print "showInt called with : ", i
10
11		
12 x = extends()
13 x.showInt(6)
14 x.showSix()
------------------------------------------------ Extends.jy

The output is as expected:

Jython class 'extends' initialised
Java Class Abstract Constructing
Java Class Abstract Constructed
showInt called with :  6
showInt called with :  6



However, if I decomment line 5 in Abstract.java, so that the system
attempts to call the abstract method in the constructor the output is
an error:

Jython class 'extends' initialised
Java Class Abstract Constructing
Traceback (innermost last):
  File "D:\gedbs\Java\lib\uk\co\RushCoding\extend.jy", line 12, in ?
AttributeError: abstract method "showInt" not implemented


Obviously this can be solved by using an explicitly called init()
function rather than the constructor, but since I'm actually extended
a public api I'd prefer to avoid this.

Is there any way of calling an extended abstract method in the
constructor, or preventing the abstract classes default constructor
from being called?




More information about the Python-list mailing list