This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Interface Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Interface Objects" Watch "Interface Objects" New topic
Author

Interface Objects

Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
I am a bit confused on the following:

Collection is an interface and ArrayList is a concrete class.Then

Collection c =new ArrayList(); ......................(1)

How above(1) is posible as interface objects cannot be created.What could be the idea behind the above instruction(1)


Thanks,
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

You're creating an instance of the class ArrayList, and you're holding it in a variable of type "Collection". This is like buying a pet of type "Goldfish" and putting it in a fish tank. You can't buy a "Fish" -- you can only buy an Angelfish, a Goldfish, a Guppy, etc. But the tank can hold any kind of Fish.

A Collection variable can hold any kind of Collection -- and ArrayList, which implements List which extends Collection, is a kind of Collection.


[Jess in Action][AskingGoodQuestions]
Phillipe Rodrigues
Ranch Hand

Joined: Oct 30, 2007
Posts: 165
What about the below line numbers for the below code.When do we use them.What could be the possible cause for assiging class objects to an interface reference?

 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Interface Objects
 
Similar Threads
from mock test
Ordered and Sorted
synchronized lock/unlock -> deadlock?
ArrayList class
Iterating through a collection that contains user defined objects