Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Instantiating Interfaces?

 
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ranchers;)

As I am now delving more and more into Polymorphism,
I come across Interfaces. I learned that they cannot be
instantiated, until this question gave me some doubts:

public interface IFace {



It was asked what the output would be, and I thought that
it must give a Compiler Error because of



but the output was

IFace
static

when I treid it out. Why so? I thought you cannot instantiate
Interfaces...does it have to do something with the brackets
{} right after the Instantiation?

King regards
Florian
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Florian Jedamzik wrote:I thought you cannot instantiate
Interfaces...does it have to do something with the brackets
{} right after the Instantiation?


Yes, it does. You are not instantiating the interface; you are declaring an anonymous class that implements the interface, and you are creating an instance of that anonymous class.
 
Greenhorn
Posts: 18
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

To support Jesper de Jong answer : Yes you are creating a anonymous Class that is implementing the interface.

You can check your bin folder a .class file will be created with
Go$1.class.

 
Florian Jedamzik
Ranch Hand
Posts: 47
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much;)
    Bookmark Topic Watch Topic
  • New Topic