jython question: accessing protected superclass methods

Ben Hutchings ben.hutchings at roundpoint.com
Mon Mar 5 14:10:40 EST 2001


Michael Vanier <mvanier at endor.bbb.caltech.edu> writes:

> Hi,
> 
> So, I've been playing with jython for a few hours, and I'm *really*
> impressed.  This is a kick-ass system for doing portable graphics
> programming.
> 
> However, the online docs are a bit sparse, and one problem I've been having
> is in trying to call a protected superclass method from a subclass which
> overrides the method.  The offending superclass is javax.swing.JPanel.  I
> have a subclass of this (let's call it MyJPanel) which needs to override the
> protected paintComponent() method of JPanel.  The naive approach:
> 
> class MyJPanel:
>     # ... lots of code ...
> 
>     def paintComponent(self, g):  # g == Graphics object
>         JPanel.paintComponent(self, g)
>         # extra code goes here
> 
> doesn't work, because paintComponent is not accessible from the MyJPanel
> namespace.  The online jython docs say this:
> 
>     In Python, if I want to call the foo method in my superclass, I use the
>     form:
> 
>         SuperClass.foo(self)
> 
>     This works with the majority of methods, but protected methods cannot be
>     called from subclasses in this way.  Instead you have to use the
>     "self.super_foo()" call style.

> 
> Well, I couldn't get this to work.  I'm not sure if the above
> statement means that you're supposed to write (in my case)
> "self.super_paintComponent(g)" or self.JPanel_paintComponent(g) but
> neither one works.  What is the magic invocation?
> 
> Also, I was surprised to find that MyJPanel, just by subclassing
> JPanel, gets its own public version of the protected JPanel methods!

That's what 'protected' means.

> So if I don't override the method, I can call it directly.  I assume
> this is intentional,
> but maybe the jython developers can comment.
> 
> Thanks,
> 
> Mike
> 
> --------------------------------------------------------------
> Mike Vanier	mvanier at bbb.caltech.edu
> Department of Computation and Neural Systems, Caltech 216-76

-- 
Any opinions expressed are my own and not necessarily those of Roundpoint.



More information about the Python-list mailing list