简体   繁体   中英

how to instance generic abstract class

I am trying to profile a ann algorithm written in Java that is implemented as a generic abstract class and I cant figure out how to instance it.

Eclipse gives me error "Cannot instantiate the type KdTree" which is not very helpful. Any ideas on how to instance this class so I can test it?

Class defination and constructor:

public abstract class KdTree<T> {    
    private KdTree(int dimensions, Integer sizeLimit) {
        this.dimensions = dimensions;
    }
}

My attempt to instance it:

public class test_robo {
    public void run_test() 
    {
        KdTree<Integer> tree = new KdTree<Integer>(1,1);
    }
}

link to the full code for KdTree http://robowiki.net/wiki/User:Rednaxela/kD-Tree

First of all, you cannot instantiate an abstract class.

I saw the code in the link you provided; there are few implementations of the base class KdTree<T> already in there.

  1. WeightedSqrEuclid
  2. WeightedManhattan
    ...

If that's not what you're looking for, extend the base class and implement all those abstract methods as you wish.

You cannot instantiate an abstract class directly. The reason it is declared abstract is that it is not meant to be used by itself - you have to provide an implementation of its abstract methods first.

You need to inherit your own class from the abstract base, implement its abstract methods, and then instantiate your class. An instance of your class is automatically an instance of its abstract base.

public class ProfilerTree extends KdTree<Integer> {
    public ProfilerTree(int dimensions, Integer sizeLimit) {
        super(dimensions, sizeLimit);
    }
    ...
    // Implement abstract methods of KdTree<Integer> here
}
...
KdTree<Integer> tree = new ProfilerTree(1,1);

you can't instantiate an abstract class. Abstract actually means it doesn't make sense on its own so it always has to be extended and its methods implemented.

Unlike interfaces, abstract classes can contain fields that are not static and final, and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead. Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable or Cloneable, for example. By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).

see http://docs.oracle.com/javase/tutorial/java/IandI/abstract.html

You can instantiate it by constructing an anonymous subclass, like so:

KdTree<Integer> tree = new KdTree<Integer>(1,1)
{
    @Override
    public void myAbstractMethodName()
    {
        //do something!
    }
};

Otherwise, you can generate your own implementation:

private class KdTreeSub extends KdTree<Integer>
{
    public KdTreeSub()
    {
        super(1, 1);
    }
}

And later call it

public void myMethod()
{
   ...
   KdTree<Integer> kdtree = new KdTreeSub();
   ...
}

The reason for this is that abstract classes are not complete classes. They are missing parts of them, usually a method. This method is marked with the "abstract" identifier:

public abstract int read();

The idea behind this is that you can construct a class that handles other parts:

public byte[] read(int len)
{
    byte[] b = new byte[len];
    for(int i = 0; i < b.length; i++) b[i] = read();
    return b;
}

And simplify creating new classes.

The class, as it stands, was not meant to be instantiated. It's meant to store boilerplate code for concrete implementations. There are 4 of them in your link, starting with WeightedSqrEuclid .

You can either instantiate those, simply by eg new WeightedSqrEuclid<Integer>(1,1) , or, if you want to profile the general code, write your own class extending KdTree .

However, in the latter case you should either create your subclass in the same file, or change a constructor of KdTree to at least protected . This is because, to create a subclass of this type, you need to call one of the constructors of KdTree in your implementation.

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