• 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

Anonymous inner class doubt

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does anonymous class cannot extend a class explicitly ??
also does anonymous class cannot implement an interface explicitly??

IS anonymous class implicitly final??
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Does anonymous class cannot extend a class explicitly ??


The simple answer is NO, but when you start defining an anonymous class, you implicitly are extending a class.

also does anonymous class cannot implement an interface explicitly??


Again, the same explanation as above.

IS anonymous class implicitly final??


First of all, one cannot have a constructor for an anonymous class. There is no pre-existing class name that is defined for an anonymous class, for one to even declare it as final.

I am sure I will be corrected if I'm wrong in what I just explained, but I think I'm close.

Thanks,
-Vijay
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's also worth noting that an anonymous inner class can either extend another class or implement a single interface.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more point, may be irrelevant, anonymous classes do have a class file. They are named in the following manner, OuterClass$1, OuterClass$2 etc.

That is,


will give A.class and A$1.class.

regards,
vijay.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget the following aren't allowed :

class A{
}
interface B{
}
class C{
B b = new B(){
};
A a = new A(){
};
A c = new A() implements B{ //can't implement an interface
};
B d = new B() extends A{ //can't extend a class
};
}
The compiler says :
';' expected
A c = new A() implements B{
^
';' expected
B d = new B() extends A{
^
2 errors

Processus termin� avec code quitter 1
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic