Reputation: 11
How I can instantiate a object in the List e.g
i like to search for begin in the file , if it finds then add the store the code after it. here is the example
public abstract class Structure
{
private List<Structure> structureList = new ArrayList<Structure>();
protected void setStructure(Filemanager file, String line)
{
/*
* set all values at the object structure
*/
line = file.getSourceLine();
while (!line.contains("END_"))
{
if (line.contains("TRANSLATE"))
{
}
else if (line.contains("BEGIN_OBJECT"))
{
structureList.add(new FunctionalStructure(line));
}
else
{
setValue(line);
}
line = file.getSourceLine();
}
}
protected abstract void setValue(String line);
}
public abstract class FunctionalStructure extends Structure
{
private String name;
public FunctionalStructure(String line)
{
super();
this.name = line.substring(line.indexOf("\"")+1, line.lastIndexOf("\""));
}
public String getName()
{
return this.name;
}
public List<Structure> Startfunctional()
{
return null;
}
protected abstract void setValue(String line);
}
I have problem in in instantiate structureList.add(new FunctionalStructure(line));
Can anyone please tell what is wrong in the above line
Upvotes: 0
Views: 115
Reputation: 16235
I think that FunctionalStructure
must be an abstract class (which presumably extends Structure
). You cannot instantiate an abstract class.
This is why you get the error like:
Cannot instantiate the type FunctionalStructure
If you created the FunctionalStructure class, perhaps you accidentally marked it as abstract. Assuming it implements the setValue(String) method, you could remove the abstract modifier from the class declaration.
Alternatively, use a suitable concrete class extending FunctionalStructure
in the API you are using.
Alternatively, use an anonymous inner class:
structureList.add(new FunctionalStructure(line){
public void setValue(String value) {
// your implementation here
}
});
Upvotes: 1
Reputation: 1609
you might find example below helpful to understand the concept :-
package pack;
public class Demo {
public static void main(String[] args) {
MyGeneric<A> obj = new MyGeneric<A>() ;
//obj.add(new C()) ; //don't compile
obj.testMethod(new A()) ; //fine
obj.testMethod(new B()) ; //fine
}
}
class A{}
class C{}
class B extends A{}
class MyGeneric<T>{
public void testMethod(T t) {
}
}
EDIT : So, there must be a IS-A relation between Structure and FunctionalStructure to successfully compile the code.
Upvotes: 0