| Author |
Instantiating an interface
|
Fantine Ponter
Ranch Hand
Joined: Aug 11, 2008
Posts: 30
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Balu Sadhasivam
Ranch Hand
Joined: Jan 01, 2009
Posts: 874
|
|
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
Joined: Aug 11, 2008
Posts: 30
|
|
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
Joined: Aug 11, 2008
Posts: 30
|
|
|
Thanks Balu. I need to find a better rule in my head for '=new' and/or 'instantiate'.
|
 |
 |
|
|
subject: Instantiating an interface
|
|
|