• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I instantiate an Interface?

 
Jane Somerfield
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is No. But I can do this

List<int> l = new List<int>();

Why?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you really compile that code?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating an anonymous inner class is nothing but implementing that particular interface and you would have to implement all the methods present in the interface.

The given code is no where close to that. Moreover the type used in the Generic declaration has to be a class/reference type.
 
Rob Spoor
Sheriff
Pie
Posts: 20546
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:Can you really compile that code?

It won't. Even if there were a non-abstract List class with a generic type (java.util.List is an interface, java.awt.List is not generic), the <int> is still not allowed because primitive types can never be generic types.
 
Jane Somerfield
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, the code does not compile. My question was can we use primitive in generics.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane Somerfield wrote:Sorry, the code does not compile. My question was can we use primitive in generics.

I think Rob just mentioned about this in his post and also I have mentioned it in my second post.

I was struggling to remember the "primitive" keyword though
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane Somerfield wrote:My question was can we use primitive in generics.

No.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic