This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can I instantiate an Interface?  RSS feed

 
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
Bartender
Posts: 3169
34
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
Bartender
Posts: 3169
34
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
Posts: 20893
81
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
Bartender
Posts: 3169
34
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
Posts: 10573
65
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!