File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes If set is interface then how it is instantiated: Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "If set is interface then how it is instantiated:" Watch "If set is interface then how it is instantiated:" New topic
Author

If set is interface then how it is instantiated:

yamini nadella
Ranch Hand

Joined: Apr 13, 2004
Posts: 257
import java.io.*;
import java.util.*;
public class collection
{
public static void main(String args[])
{
collection obj = new collection();
obj.start();
}
void start()
{
Map map = new HashMap();
map.put("raja",new Integer(32));
map.put("raja1",new Integer(33));
map.put("raja2",new Integer(55));
map.put("aja3",new Integer(39));

Set set = map.keySet();
if (set.contains("raja"))
System.out.println("contain raja");
System.out.println("hello "+set.toString());
Object[] o = set.toArray();
System.out.println("hello "+set.toString());

}
}
yamini nadella
Ranch Hand

Joined: Apr 13, 2004
Posts: 257
I am adding part of above question here.
According to my idea interface contains methods with no body. Here Set is interface. I did not implement set, but I am able to create a set object.
and I am able to use set methods. where these methods are programmed?.
C. Magmanum
Ranch Hand

Joined: Apr 03, 2004
Posts: 35
most class that implement the Collection interface are found in the java.util package. The most generally used implementation for Set, for instance, are the HashSet and TreeSet. Dont get confused, the Collection interface and Collections classes are both part of the so called collection framework.

what u r actually doing during instantiation is creating an object of the class(for instance HashSet) but setting the type as an interface(in this case Set) as the reference type.
...I hope I am making sense and I hope it helps
[ April 14, 2004: Message edited by: Crusty Magmanum ]
C. Magmanum
Ranch Hand

Joined: Apr 03, 2004
Posts: 35
*errata* lest I lead u astray
...Most commonly used implementation of Set are HashSet and TreeSet
most commonly used implementation of List are ArrayList and LinkedList
and commonly used Map implementations are HashMap and TreeMap.
HashSet, TreeSet, ArrayList, LinkedList, HashMap and TreeMap ...I think they are all in the java.util package.
The Collection class contains static methods that allow for manupulation of the implementations
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
You can have references variables that have a type of an interface (which is not instantiable) just as you can have reference variables that have a type of an abstract class (which is also not instantiable). They key thing to remember is that the object that the variable refers to is some class that implements that interface or extends that abstract class.
For your example:

When you invoke the keySet method, that method, which is defined within HashMap, returns some object (you don't really know the type of that object) that implements the Set interface. Like I said, you don't really know what type of Object that is - all you know is that it implements Set. Because it implements Set, though, you know that you can safely invoke any methods on that object that are defined in Set.
I hope that helps,
Corey


SCJP Tipline, etc.
Lionel Orellana
Ranch Hand

Joined: Mar 19, 2004
Posts: 87
Originally posted by yamini nadella:
I am adding part of above question here.
According to my idea interface contains methods with no body. Here Set is interface. I did not implement set, but I am able to create a set object.
and I am able to use set methods. where these methods are programmed?.

You are not creating a set object. When you do Set set = map.keySet(); the method keySet in map is creating an object that implements Set and you're using a variable of type set to refer to that object. The actual type of the object might be LinkedHashSet or any other that implements Set. You don't need to know what type it is, you just know it's a set and that's the beauty of it all. It's in that object where the methods are implemented, not in Set.
It's exactly the same as doing Map map = new HashMap(); in your sample, only that here you can see directly where the HashMap is created. Map is an interface but you are not creating an instance of map, you are creating an instance of HashMap. Well, it's the same with the set, you just don't see where the LinkedHashSet (or whatever it is) is being created because it happens inside the method keySet().
Made sense?
[ April 14, 2004: Message edited by: Lionel Orellana ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: If set is interface then how it is instantiated: