• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiating Interfaces?  RSS feed

 
Florian Jedamzik
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
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • 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.
 
Vaishakh Menon
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;)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!