Does Java allow to do something similar to this:
Abstract.java:
public abstract class Abstract {
int state;
abstract void changeState(int newState);
public static class Inherited extends Abstract {
void changeState (int newState) {
/* ... */
}
}
}
Base.java:
public class Base {
static HashMap<String, Abstract> map;
static {
map.insert("handle", new Abstract.Inherited());
}
}
When I try to compile it, I get: "The method insert(String, Abstract.Inherited) is undefined for the type HashMap String,Abstract". I know I can resolve it by moving Inherited completely out of Abstract, but is there a way to preserve it?
Your problem has nothing to do with nested classes, and everything to do with the fact that the method in Map
is called put
, not insert
.
What you're trying to do is entirely valid. After changing insert
to put
and importing java.util.HashMap
in Base.java
, your code compiles.
See more on this question at Stackoverflow