• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner Class inheritance

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I know the inner class can extend other class or implement an interface. How can I do that when the inner class are anonymous and don't have name or not implicit contructor ? Please use a small code as an example.
Thanks in advance
Ricardo
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An inner class doesn't HAVE to be anonymous, but it can be.

/Mike
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ricardo,
Mikael is correct; an inner class doesn't have to be anonymous, however, if you want to create an anonymous inner class jus use the name of the class it will extend or the interface it will implement.
For example,

Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited June 11, 2001).]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ricardo
To extend a class or implement an interface in an anonymous you would use code similar to this:

in this case your creating a new anonymous class that implements the ActionListener interface. There is no class name (that's why they are called anonymous). You just supply the name of the class or interface you are extending or implementing. That's why in anonymous classes you can only extend one class or implement one interface.
hope that helps
Dave
for more details, check this out: http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#252986
 
Ricardo Polero
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks a lor , but still I am a little confused: how the compiler choose between the classes/interfaces if for example in the inherance's tree this methods are overriding? (without the name of the "leaf: in the tree. )
Thanks in advance
Ricardo
 
Ricardo Polero
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I mean in the anonymous inner class case !
Ricardo
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ricardo,
The compiler just uses the class or interface name as the starting point and then applies the overriding rules. It will use the newly defined methods of the class while the class is loaded. In Dave's example, any reference to the ActionListener for 'myButton' will automatically use the 'actionPerformed' method defined in the anonymous ActionListener class.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic