Caner Caner - 1 month ago 14
Java Question

Java: Cannot instantiate the type Foo

I'm trying to create my own version of

HashMap
with some utility methods.

Foo.java:

import java.util.HashMap;

public class Foo<String, Parameter> extends HashMap<String, Parameter> {

public Foo() {
super();
}

public Parameter Add(String key, MyType type) {
return put(key, new Parameter(type)); // -> This line causes compilation error
}
}


The following line:

new Parameter(type);


produces
Cannot instantiate the type Foo
.

I checked
Parameter
class and it is not an abstract class/interface
, why am I getting this error?

EDIT

Changing class declaration as following solved the problem:

public class Foo extends HashMap<String, Parameter> {

Answer

It's not an abstract class - it's a type parameter at the moment, as is String! Your class is generic, with two type parameters. I believe you meant:

public class Foo extends HashMap<String, Parameter> {

Now it's a non-generic class.