Working with Java Constructors

WOes you saCc!ess e Jene cless, yo u cell itc superclass's cosstractor, just es you do is regtlfn PytOos. As we saw, the example subclassed the Date class from j ava .lang and defined two constructgrs that calledDate's base class constructors.

As ar exercise, look up the constmetors of j ava . lang. Date in the Java API documentation. You'll find them in a section catted Constructor Summary.

The constructor of Date is defined in Java as

You cell it from Python like this:

The second constructor of Date is defined in Java as

Date (long date)

You call it from Python like this:

Here's how to call both Date constructors:

class MyDate(Date):

if num is None:



In Jeve, constructors are special language constructs thet elweys lave the seme name es that of their class. In

Python, tie constructor is always a method called_init_. You call Js. constructors in Python lust as you do Python constructore; however, ymu can o^y do so in your soeclass's constructor.

Was this article helpful?

0 0

Post a comment