wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Is Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is "array of interface" supported in Java ?" Watch "Is "array of interface" supported in Java ?" New topic
Author

Is "array of interface" supported in Java ?

Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
Hi ranchers !
Is such thing as
array of interface
supported in Java ?

I understand that I can create array of Object and then cast
each element to my interface.
I understand also that in incoming java 1.5 will be generics.
(that not only make the language more complex, but
(prabably) can help in the case)
I understand also about array of base class, but I wish exactly
array of interface,
(the elements of the array can be without common base class,
(only Object can help here, because it alwais base class for all))
;-)
Really I think that such thing is not implemented in Java, but... ?
Thank you !
[ November 30, 2003: Message edited by: Igor Ko ]
[ November 30, 2003: Message edited by: Igor Ko ]
[ November 30, 2003: Message edited by: Igor Ko ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

In Java, you can define an array of any type. As interfaces are types, you can indeed define arrays of interfaces -- i.e.,
Cloneable[] c = new Cloneable[10];
declares and constructs an array that can hold ten objects that implement the "Cloneable" interface.


[Jess in Action][AskingGoodQuestions]
Igor Ko
Ranch Hand

Joined: Jun 24, 2002
Posts: 90
Thank you. It works !
[ December 01, 2003: Message edited by: Igor Ko ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Is "array of interface" supported in Java ?
 
Similar Threads
JAVA RAndom Numbers
Java still Java?
Calling main() method
page 159 of Sierra/Bates book
A "Class" problem, someone help me...