O'Reilly logo

Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython by Richard Hightower

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required