Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Instantiating an interface

 
Fantine Ponter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Head First Java 2nd edition, on page 232, pool puzzle (page 234 puzzle solution).

My understanding of an abstract 'thing', is that it can never be instantiated.
All interfaces are implicitly public abstract.
An array is an object
'= new' means to instantiate an object

Please can someone explain how one can say the following when Nose is an interface?


Here is the code that's pertinent to the question:
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fantine Ponter wrote:Please can someone explain how one can say the following when Nose is an interface?

You're not instantiating an interface - you are creating an array that can store objects of any class, as long as that class or one of its ancestor classes implements that interface. In your case it can contain both Picasso and Clown objects.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Fantine Ponter wrote:
Please can someone explain how one can say the following when Nose is an interface?




This is as similar to int[] a = new a[5]; , no way one can create a object for primitive type , its just creating array of int.

 
Fantine Ponter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.

I've just found the answer myself, on page 207... oops! if only i would remember completely instead of partially.
"Don't panic. We're not making a new Animal object; we're making a new array object, of type Animal. (Remember, you cannot make a new instance of an abstract type, but you CAN make an array object declared to HOLD that type.)"
 
Fantine Ponter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Balu. I need to find a better rule in my head for '=new' and/or 'instantiate'.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic