This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Need clarifications on interface's object initialization  RSS feed

 
Greenhorn
Posts: 3
Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am preparing for OCAJP 8 and currently doing mock test to prepare for the final exam. I have a question which i want to clarify with the forum here:

Today, i encountered below code regarding instantiation of an interface and couldn't get my head around how this code work? Are we creating any object for an Interface I below or is it just a way to bypass/override the object creation process?


 
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the reason it works is because the interface has a default implementation.  If you add another method signature in there without an implementation then I don't think it will work.
 
Al Hobbs
Rancher
Posts: 427
6
Fedora IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm guessing the way it works is it makes an anonymous class object, but I don't actually know. I'm thinking that because if the interface had an unimplemented method then the only way you would be able to run that is if you created an anonymous class when you created the interface.
 
Enthuware Software Support
Posts: 4308
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikhil Shrivastava wrote:Hi,
I am preparing for OCAJP 8 and currently doing mock test to prepare for the final exam. I have a question which i want to clarify with the forum here:

Today, i encountered below code regarding instantiation of an interface and couldn't get my head around how this code work? Are we creating any object for an Interface I below or is it just a way to bypass/override the object creation process?



It defines and instantiates an anonymous class that implements I in a single statement. Read more here: https://www.geeksforgeeks.org/anonymous-inner-class-java/
 
Master Rancher
Posts: 4087
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the classes generated when you compile you'll see that th compiler has created a class called something like Book$1, which is the anonymous class from line 14.
 
I RELEASE YOU! (for now .... ) Feel free to peruse this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!