Subclassing Java Classes

You can subclass Java classes like Python classes. Here's an example of subclassing the Java class java.util.Date (from MyDate.py):

from java.util import Date

class MyDate(Date):
       def __init__(self, num=None):
             if num is None:
                    Date.__init__(self)
             else:
                    Date.__init__(self, num)
       def __str__(self):
             str = Date.toString(self)
             str = "MyDate: " + str
             return str

Working with Java Constructors

When you subclass a Java class, you call its superclass's constructor, just as you do in regular Python. As we saw, the MyDate.py example subclassed the Date class from java.lang and defined two constructors that called Date's base class constructors.

As an exercise, look up the constructors of java.lang.Date in the Java API documentation. ...

Get Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.