• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Need clarifications on interface's object initialization

 
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: 478
6
IntelliJ IDE Spring Fedora
  • 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: 478
6
IntelliJ IDE Spring Fedora
  • 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: 4338
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: 4214
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.
 
All of the following truths are shameless lies. But what about this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!