From: D-Man <ds...@ri...> - 2001-03-26 15:17:53
|
On Mon, Mar 26, 2001 at 09:21:19PM -0500, cindy wrote: | Hi, | The book I'm using to learn Java has a statement | in one of its examples that is "super(title)". | This statement is in a module that extends Frame. What | qualifiers do I need for "super" and what do I have to import? Java only supports single inheritance. "super" is a keyword in Java that refers to a super class of the current class (the first super class with the specified method). Here is an example, first in Java then Python (Jython -- same grammar): public class MyFrame extends java.awt.Frame { /** * A ctor for this class. Must call Frame's ctor to init it as * well. */ public MyFrame( ) { // call java.awt.Frame's ctor super( ) ; } } # bring the "java" module into the current namespace import java class MyFrame( java.awt.Frame ) : # A initializer (ctor) for this class. Must call Frame's ctor to # init it as well. def __init__( self ) : # call the java.awt.Frame's ctor java.awt.Frame.__init__( self ) The main difference is that Python supports multiple inheritance. Thus the meaning of "super" in python isn't quite clear and if a particular meaning is settled on it may not work as expected/intended in some cases. As a result python's designers didn't include "super" as a keyword, but instead you have to specify the super class explicitly by name. The end result is the same, just a liitle different look to the code. BTW, I just tested the python part, and Jython very nicely translated the python idiom of calling __init__ into the java ctor of Frame (as obviously java.awt.Frame doesn't have a method named __init__). HTH, -D |