Cannot be instantiated error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • champion
    New Member
    • Mar 2007
    • 5

    Cannot be instantiated error

    I am the following when i try to run my java program. I have included the code below

    CollectionExamp le1.java java.util.Colle ction is abstract; cannot be instatiated
    Collection col=new Collection();

    CODE
    Code:
    import java.util.*;
    
    class CollectionsExample1
    {
    
    static void CollectionMethod()
    
    {
      Collection col=new Collection();
      
      col.add("J");
      col.add("e");
      col.add("s");
      col.add("u");
      col.add("s");
    
      boolean cc=col.contains("g");
      System.out.println("\n");
      System.out.println(cc);
      System.out.println("\n");
    
      col.remove("u");
      System.out.println("u has been removed");
      System.out.println("\n");
    
      Iterator gh=col.iterator();
      while(gh.hasNext())
       {
        Object element=gh.next();
        System.out.println(element);
       }
    }
    
    
    
    static void SetsMethod()
    {    
    
        // Create the set
        Set set = new HashSet();
        
        // Add elements to the set
        set.add("L");
        set.add("O");
        set.add("R");
        set.add("D");
        set.add("D");    
    
    
        // Get number of elements in set
        int size = set.size();         
        System.out.println("The size of the Set is "+size);    
    
    
        //Adding an element that already exists in the set has no effect
        System.out.println("Adding O has no effect on size of the set");
        set.add("O");
        size = set.size(); 
        System.out.println("The size of the Set is "+size);    
                 
        
        // Determining if an element is in the set
        boolean b = set.contains("L");  
        System.out.println(b);
    
        b = set.contains("c");          
        System.out.println(b);
        
        // Iterating over the elements in the set
        Iterator it = set.iterator();
        while (it.hasNext()) 
        {
            // Get element
            Object element = it.next();
    	System.out.println(element);
        }
        
        // Remove elements from the set
        set.remove("D");
    
        // Create an array containing the elements in the set (in this case a String array)
        String[] array = (String[])set.toArray(new String[set.size()]);
    
        for(int i=0;i<set.size();i++)
          System.out.println(array[i]);
    
    }
    
    
    static void ArrayListMethod()
    {
        
    
       // Create the sorted set
        SortedSet set = new TreeSet();
        
        // Add elements to the set
        set.add("b");
        set.add("c");
        set.add("a");
        set.add("z");
        set.add("a");    
    
        // Iterating over the elements in the set
        Iterator it = set.iterator();
        while (it.hasNext()) 
        {
            // Get element
            Object element = it.next();
    	System.out.println(element);
        }
        // The elements are iterated in order: a, b, c
        
        // Create an array containing the elements in a set (in this case a String array).
        // The elements in the array are in order.
        String[] array = (String[])set.toArray(new String[set.size()]);
        
        System.out.println("\n\n");
    
    
        for(int i=0;i<set.size();i++)
          System.out.println(array[i]);
    
    }
    
    public static void main(String [] args)
      {
        SetsMethod();
        ArrayListMethod();
        CollectionMethod();
      } 
    
    }
    Last edited by horace1; Mar 22 '07, 09:13 AM. Reason: added code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Collection is an interface so you cannot create an object
    Code:
    static void CollectionMethod()
    {
      Collection col=new Collection();
    you need to use something that implements it, e.g.
    Code:
      Vector col=new Vector();

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      You can also write

      Code:
      Collection col = new Vector();
      or any other Collection-extending class like List, Set, Tree, etc. in place of the class name on the right. In this case, though, you can only use the methods that all Collections have.

      Comment

      Working...