This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Anonymous Inner class extends/implements  RSS feed

 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, This question(removed question), answer and explanation is from K&B mock exam. Could you please show sample code because I am not able to make it.

Answer:

Anonymous inner class can extend exactly one class or implement exactly one interface.

EXPLANATION:
The syntax of an anonymous inner class allows for only one named type
after the new, and that type must be either a single interface (in which case the anonymous
class implements that one interface) or a single class (in which case the anonymous class
extends that one class).
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best example is the way we add ActionListener to the Swing components:



ActionListener is an Interface which contains only one method- actionPerformed. In the above code- we are using an Anonymous Inner class- Actually implementing the interface- ActionListener. And you can see that we are able to implement only interface. Similar way you could be using some concrete class to Extend the class.
 
Ranch Hand
Posts: 186
C++ Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can figure out the crap on line 14..


 
Ranch Hand
Posts: 33
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please see following classes doing the same thing in different ways

Case I: Without using Anonymous Inner Classes.



With this version of code you can do the following things
1. You can implement not only runnable interface but multiple interfaces using comma separated list of interface names. This is not possible in Case II and III versions.
2. You can extend one class and implement multiple interfaces. This is also not possible in II and III.

Case II: Anonymous Inner Class Version with implementing only one Runnable interface



Case III: Anonymous Inner Class Version with extending only one Thread class



Hope this will clear your understanding.

Thanks,
Kushan
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kushan.
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!