简体   繁体   中英

Can't instantiate AbstractSet Class

Ok I've been searching for this problem for a while. I keep getting an error that I can't instantiate abstractSet . It keeps asking for a generic. I add the generic but still no dice.

import java.util.AbstractSet;
import java.util.Set;
public class UnorderedTree{

private Object root;
private Set subtrees; //Switched to AbstractSet
private int size;

public UnorderedTree(Object root){
     this(root);
     subtrees = new AbstractSet(); //ERROR HERE WITH <Object>
     size=1;
   }
}

Any pointers would help

As its name suggested, AbstractSet is abstract, you can not instantiate it. as its javadoc said:

This class provides a skeletal implementation of the Set interface to minimize the effort required to implement this interface.

You should use some concrete set like HashSet .

AbstractSet is an "abstract" base class, which cannot be instantiated. It has a protected constructor which is overridden by the classes that extend this abstract class.

Please look at the javadoc for more help.

Direct known subclasses of this class are: ConcurrentSkipListSet, CopyOnWriteArraySet, EnumSet, HashSet, TreeSet - You are probably interested in one of these.

Good luck!

AbstractSet is an abstract class. You can't instantiate abstract classes, because they (generally) don't contain a complete implementation of the type they're defining. It has nothing to do with generics.

You can't initialize abstract classes.

From Java 6 API this is the definition of the AbstractSet class.

public abstract class AbstractSet<E>
extends AbstractCollection<E>
implements Set<E>

For more information please refer Java API documentation .

Your can overcome this compilation error by using:

private Set<YourObjType> subtrees = new HashSet<YourObjType>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM