473,757 Members | 3,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot be instantiated error

5 New Member
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
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. class CollectionsExample1
  4. {
  5.  
  6. static void CollectionMethod()
  7.  
  8. {
  9.   Collection col=new Collection();
  10.  
  11.   col.add("J");
  12.   col.add("e");
  13.   col.add("s");
  14.   col.add("u");
  15.   col.add("s");
  16.  
  17.   boolean cc=col.contains("g");
  18.   System.out.println("\n");
  19.   System.out.println(cc);
  20.   System.out.println("\n");
  21.  
  22.   col.remove("u");
  23.   System.out.println("u has been removed");
  24.   System.out.println("\n");
  25.  
  26.   Iterator gh=col.iterator();
  27.   while(gh.hasNext())
  28.    {
  29.     Object element=gh.next();
  30.     System.out.println(element);
  31.    }
  32. }
  33.  
  34.  
  35.  
  36. static void SetsMethod()
  37. {    
  38.  
  39.     // Create the set
  40.     Set set = new HashSet();
  41.  
  42.     // Add elements to the set
  43.     set.add("L");
  44.     set.add("O");
  45.     set.add("R");
  46.     set.add("D");
  47.     set.add("D");    
  48.  
  49.  
  50.     // Get number of elements in set
  51.     int size = set.size();         
  52.     System.out.println("The size of the Set is "+size);    
  53.  
  54.  
  55.     //Adding an element that already exists in the set has no effect
  56.     System.out.println("Adding O has no effect on size of the set");
  57.     set.add("O");
  58.     size = set.size(); 
  59.     System.out.println("The size of the Set is "+size);    
  60.  
  61.  
  62.     // Determining if an element is in the set
  63.     boolean b = set.contains("L");  
  64.     System.out.println(b);
  65.  
  66.     b = set.contains("c");          
  67.     System.out.println(b);
  68.  
  69.     // Iterating over the elements in the set
  70.     Iterator it = set.iterator();
  71.     while (it.hasNext()) 
  72.     {
  73.         // Get element
  74.         Object element = it.next();
  75.     System.out.println(element);
  76.     }
  77.  
  78.     // Remove elements from the set
  79.     set.remove("D");
  80.  
  81.     // Create an array containing the elements in the set (in this case a String array)
  82.     String[] array = (String[])set.toArray(new String[set.size()]);
  83.  
  84.     for(int i=0;i<set.size();i++)
  85.       System.out.println(array[i]);
  86.  
  87. }
  88.  
  89.  
  90. static void ArrayListMethod()
  91. {
  92.  
  93.  
  94.    // Create the sorted set
  95.     SortedSet set = new TreeSet();
  96.  
  97.     // Add elements to the set
  98.     set.add("b");
  99.     set.add("c");
  100.     set.add("a");
  101.     set.add("z");
  102.     set.add("a");    
  103.  
  104.     // Iterating over the elements in the set
  105.     Iterator it = set.iterator();
  106.     while (it.hasNext()) 
  107.     {
  108.         // Get element
  109.         Object element = it.next();
  110.     System.out.println(element);
  111.     }
  112.     // The elements are iterated in order: a, b, c
  113.  
  114.     // Create an array containing the elements in a set (in this case a String array).
  115.     // The elements in the array are in order.
  116.     String[] array = (String[])set.toArray(new String[set.size()]);
  117.  
  118.     System.out.println("\n\n");
  119.  
  120.  
  121.     for(int i=0;i<set.size();i++)
  122.       System.out.println(array[i]);
  123.  
  124. }
  125.  
  126. public static void main(String [] args)
  127.   {
  128.     SetsMethod();
  129.     ArrayListMethod();
  130.     CollectionMethod();
  131.   } 
  132.  
  133. }
Mar 22 '07 #1
2 6340
horace1
1,510 Recognized Expert Top Contributor
Collection is an interface so you cannot create an object
Expand|Select|Wrap|Line Numbers
  1. static void CollectionMethod()
  2. {
  3.   Collection col=new Collection();
  4.  
you need to use something that implements it, e.g.
Expand|Select|Wrap|Line Numbers
  1.   Vector col=new Vector();
  2.  
Mar 22 '07 #2
Ganon11
3,652 Recognized Expert Specialist
You can also write

Expand|Select|Wrap|Line Numbers
  1. 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.
Mar 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2799
by: Yasutaka Ito | last post by:
Hi folks! I have a BaseForm class that inherits System.Windows.Forms.Form. It has a property, whose value I need supplied by the class that inherits it. The BaseForm usees the value supplied into this property in its Load event. So, I gave the BaseForm and the property 'abstract' modifier, and put the implementation of the property in the inherited class; say MyForm. However, when I did this, I no longer can open MyForm in the design...
8
21723
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA. Dev
11
3723
by: Steve Franks | last post by:
I see several reference to using the ASP.NET 2.0 Cache object in C# where the documentation just shows snippets like this: if (Cache == null) ... do something However when I use this code in a C# class that is called from an ASP.NET page, the compiler gives this error: 'Cache' is a tpe and cannot be used as an expression Furthermore when I type in Cache and press the "." for intellisense, the
3
6439
by: Paul | last post by:
Hi, I'm new to .NET and I'm trying to run through the tutorial "Creating a Web Application Using VB" As I follow the directions, It seems to work just fine. I can preview the data from the SQLDataAdapter but when I try to view the data in the browser I get the error:
2
9952
by: yinglcs | last post by:
I have the following code, which use template as the parent class of my other class. But I have "instantiated from here" compile error at this line: class C: public B2<A>. Can some one please tell me why? class AI {
3
2921
by: manoj.pattanaik | last post by:
Hi, I am trying to compile following piece of code (bb.cpp) using aCC (HP ANSI C++ B3910B A.03.37) compiler on HP-UX 11.23. It gives error:485 //bb.cpp -- Starts #include <iostream> using namespace std; class abc;
8
1682
by: blisspikle | last post by:
Can any Public class be inherited from? I installed some software on my pc and I can use it in my code, but I cannot seem to inherit from it. It was an executable that installed on my pc, I do not understand how that works, but I can use it. I put it in the references to use it in my code. If I look at the definition it just says.. Public Class CABEthernet Member of: vHMIABE
1
7170
by: prakash.mirji | last post by:
Hello, I am trying to compile C++ code which uses rogue wave 9.0 classes on RHEL 4.0. I use gnu g++ compiler to compile the code. Compiler is not able to match the right type of parameters to overload the methods of RW classes. Below are the two methods among others in the rw/ep_scntn.h header file
9
2240
by: phenrol | last post by:
I am trying to access a pointer to a class that I defined in a separate source file from another one. Here is an example of what I have: //myclass.h file class testclass { public: testclass::testclass(void); int setintfunction(int testinteger); int getintfunction(void); testclass::~testclass(voide);
3
16399
by: vijishoba | last post by:
i call webpage from my windows application,the following error will occur,so i include to dlls namely SHDocVw.dll and AxSHDocVw.dll, ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment. and also i include STAthread in my main method ,any how i will get the following error, the type or namespace name 'STAThread' could not be found (are you missing a...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.