• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Anonymous class

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,there
does anyone know why an anonymous inner class can't implement an interface and in the same time extend an non-fianl class?
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When implementing an interface in an anonymous class, you are
implicitly extending the class Object. Since Java doesn't allow multiple inheritance of class implementation, you can't implement another class at the same time.
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nain,
Can you explain it in more detail.
"When implementing an interface in an anonymous class, you are
implicitly extending the class Object"
...
...
return new some_interface () { }
// that extends object??
...
public class Not_inner_class implement some_interface {}
//that extends object ??
So, what is the different?
In the original question...
as far as i know, the design of inner class not meant for
multiple inheritance cuz there is no point for it, especailly
anonymous.
Anyway, just my opinion! I do not know the answer to
"does anyone know why an anonymous inner class can't implement an interface and in the same time extend an non-fianl class?"
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think is very simple ;-)
It is not possible to use extends or implements in a anonymous class. You are limited to extend a class or implement an interface because it is only possible to place a type after the new operator.
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FEI,
Jose's answer is better.

The syntax for instantiating an anonymous class simply does not
allow you to extend a class and implement an interface at the
same time. You either use new < interface > (), which means
you extends class Object, or new < Class > (args), which means you extend the class < Class >.
But, when you declare a class implementing an interface, it is
different. You can specify which class you want to extend, if
Object is not the class you want to extend.

Hope this clarify your doubt.

[This message has been edited by Nain Hwu (edited November 10, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic