Re: Anonymous Inner class Creation

From: Kevin McMurtrie (mcmurtri_at_dslextreme.com)
Date: 03/03/05


Date: Wed, 02 Mar 2005 22:20:15 -0800

In article <1109787295.577274.178570@l41g2000cwc.googlegroups.com>,
 cbongior@stny.rr.com wrote:

> I have recently begun to experiment more with Anonymous Inner classes
> (I still consider them a devils deal). However, I did come upon a
> situation where it was nice:
>
> JPanel myPanel = new JPanel() {
> public String toString() { return "Default";}
> };
>
> I used this so I could added panels to my JComboBox and swap them on
> and off of another portion of the display. However, my hopes were
> dashed when I wanted that Anonymous Inner Class to implement and
> interface.
>
> JPanel myPanel = new JPanel() implements QueryFormatter {
> public String toString() { return "Default";}
> public String getFormat() {return "";} // interface method.
> };
>
> I got an unexpected token compile error. So, is it possible to do this
> with anonymous inner classes? I am thinking not, judging by the
> compiler errors and the fact that the lanaguage specification is silent
> on this.
>
> Comments?
>
> Christian
> http://christian.bongiorno.org/resume.PDF

Why implement that interface? You're casting the result to a JPanel.

It is possible to implement interfaces. I do it to avoid having a
public run() method for thread entry.

Runnable r= new Runnable ()
{
   public void run () {myProtectedMethod();}
};
new Thread (r).start();



Relevant Pages

  • Re: Speed of interfaces vs inheritance
    ... implementation of an interface. ... Another thing to note is the degree of actual polymorphism at the call site. ... But that kind of optimisation makes considering call sites difficult. ... abstract public void abstractCall; ...
    (comp.lang.java.programmer)
  • Re: pls clarify important concepts...
    ... >>methods in that interface.. ... > public class MyClass implements MyInterface { ... > public void myMethod() { ... > public class UserDaoJDBC implements UserDao { ...
    (comp.lang.java.help)
  • Re: Correct use of State Pattern
    ... Unless you want to use reflection, you will probably need to have a different interface for each state. ... public void becomeImpregnated; ... public Tadpole hatch; ... public Frog metamorphose() { ...
    (comp.lang.java.programmer)
  • Re: Looking for a way to avoid use of instanceOf
    ... on the front of an interface name. ... unless there's an ordering on constraints. ... public void handle; ... You then add a method to RegulatedNME: ...
    (comp.lang.java.programmer)
  • Re: oop programming
    ... class PrinterFactory{ ... public void printDocument(){ ... Return new instance (via interface). ...
    (microsoft.public.dotnet.languages.csharp)