• 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 classes must extend?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just did John Hunt's Mock exam and stumbled over this:

The correct answer is C. But must inner classes really extend any class or interface?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cristoffer,
an inner class my extend another class or implement an interface
or nothing (implicit Object)
robert
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I find the wording to be rather innacurate. The question states, "What class must an inner class extend?" For one thing, interfaces aren't classes and a class doesn't extend an interface, it implements the interface. Only interfaces can extend interfaces. So, the mention of interfaces is rather hokey with the way the question is worded.
Any class that you create in Java will necessarily extend some object. You might specify a class that it extends or, if you don't, your class will implicitly extend Object. Therefore, every inner class, like any other class, must extend some class. Of course, that can be any class you like.
Even though I dislike the wording, an inner class can also implement any interface (or number of interfaces), but it is not required to do so.
I hope that helps,
Corey
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is that true? I thought Inner class can extend one class or implement only one interface. Also, I assumed, if it is not extending or implementing, Object class is extended.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An inner class can extend another AND implement many interfaces. An anonymous class only can extend another OR implement an interface.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thiru Thangavelu:
Is that true? I thought Inner class can extend one class or implement only one interface. Also, I assumed, if it is not extending or implementing, Object class is extended.


Perhaps you're thinking of an anonymous class. When you use the following syntax to create an anonymous class:

You only have room to identify a single class or interface. However, if you make a simple inner class, like this:

You can see here that I can extend a class and also implement as many interfaces as I'd like. Of course, as with any class, if no class is extended, the Object class is extended implicitly.
I hope that helps,
Corey
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic