-
Error: an enclosing instance that contains...
I'm gettin this error that says "an enclosing instance that contains abstractClass is required."
Basically abstractClass is a subclass, and from a different class, I'm trying to create a subclass that extends it.
An internet search didn't turn up much, except one confusing article which if I understood right said this error is some sort of bug in the compiler. I couldn't really understand it very well, so that might not be the case.
Anyway, if anyone knows anything about this problem help would be appreciated. And if I need to post more code, just tell me, but I have a feeling its not that specific of a problem.
-
Ok, heres the code anyway. The abstract subclass first:
Code:
public abstract class UserDefinedElement extends Element{
public UserDefinedElement(String token){ init(token); }
protected HashMap getValueTable(){ return valueTable; }
public abstract void init(String s);
public abstract HashMap getExtraLookupValues();
}
Now the subclass trying to extend it:
Code:
private class foreachElement extends EqParser.UserDefinedElement {
EqParser main;
EqParser start;
EqParser finish;
private HashMap extraLookup;
public void init(String key){
String[] params = EqParser.getParams( key.substring(8, key.length()-1) );
main = new EqParser(params[0]);
start = new EqParser(params[2]);
finish = new EqParser(params[3]);
extraLookup.putAll(main.valueTable);
extraLookup.putAll(start.valueTable);
extraLookup.putAll(finish.valueTable);
main.valueTable = getValueTable();
start.valueTable = getValueTable();
finish.valueTable = getValueTable();
}
public HashMap getExtraLookupValues() { return extraLookup; }
public double getValue(){
int intStart = new Double(start.Evaluate()).intValue();
int intFinish = new Double(finish.Evaluate()).intValue();
if (intFinish >= oldRaces.size()) intFinish = oldRaces.size()-1;
double curValue = 0;
for(int i = intStart; i<=intFinish; i++){
main.valueTable.put("index", new Double(i));
((cOldRace)oldRaces.get(i)).FillValueTable(main);
curValue += main.Evaluate();
}
return curValue;
}
}
Theres nothing special I don't think. The error is at the declaration of the foreachElement subclass. If I create a constructor for that class it moves to there. This is really gettin to me...
Last edited by phatslug311; 07-16-2005 at 06:06 AM.
-
What is Element ? I get compile-error for UserDefinedElement as the Element class
(defined in javax.swing.text.html.parser) is final (homemade ?).
And another thing, EqParser, is that a class and a package ??
eschew obfuscation
-
Sorry for the confusion...
Element and UserDefinedElement are both subclasses of EqParser, which is just a class, not a package. Element extends DefaultMutableTreeNode.
If it helps at all to understand, EqParser is a class I'm writing that takes an equation (like "(x+5)/2") and sets up a tree of Elements(like values or operators) which can be quickly evaluated. The UserDefinedElement would be used outside of the class if I wanted to make EqParser understand something like "if(3<4,x+5)", so it needs to be overriden from outside of EqParser, which is where I seem to get this subclass extending another subclass error.
Anyway sjalle, thanks for even taking the time to read through my code (I know how much of a pain it is to try to understand someone else's) and lemme know if you have any clue what might be going on.
-
Multiple inheritance ?
This sounds like multiple inheritance to me, and I've only seen that in the
bloated hybrid OOP language called c++.
One way to achieve "multiple inheritance" is:
make an interface Z that defines all the methods of the X class that you need.
In your Y class (that extends WhatEver) you then have an object of class X.
Then define the Y class as extends WhatEver implements Z.
Then, in all the methods in Y that implements the Z interface you just call
the corresponding method for the X object.
I've made a mock version of your code that has the inheritance structure
that (I think) you described, some methods/variables may belong lower/higher
in this structure.
It may be useful, and maybe not....
Code:
import java.util.*;
import javax.swing.tree.*;
class EqParser
extends DefaultMutableTreeNode {
public EqParser() {}
public EqParser(String s) {}
public static String[] getParams(String s) {
return null;
}
public HashMap valueTable = null;
public int evaluate() {
return 0;
}
}
class Element
extends EqParser {
public Element() {}
public ArrayList oldRaces = null;
}
class COldRace {
public void FillValueTable(EqParser p) {}
}
abstract class UserDefinedElement
extends Element {
public UserDefinedElement() {}
public UserDefinedElement(String token) {
init(token);
}
protected HashMap getValueTable() {
return valueTable;
}
public abstract void init(String s);
public abstract HashMap getExtraLookupValues();
}
public class ForEachElement
extends UserDefinedElement {
EqParser main;
EqParser start;
EqParser finish;
private HashMap extraLookup;
public void init(String key) {
String[] params = EqParser.getParams(key.substring(8, key.length() - 1));
main = new EqParser(params[0]);
start = new EqParser(params[2]);
finish = new EqParser(params[3]);
extraLookup.putAll(main.valueTable);
extraLookup.putAll(start.valueTable);
extraLookup.putAll(finish.valueTable);
main.valueTable = getValueTable();
start.valueTable = getValueTable();
finish.valueTable = getValueTable();
}
public HashMap getExtraLookupValues() {
return extraLookup;
}
public double getValue() {
int intStart = new Double(start.evaluate()).intValue();
int intFinish = new Double(finish.evaluate()).intValue();
if (intFinish >= oldRaces.size())
intFinish = oldRaces.size() - 1;
double curValue = 0;
for (int i = intStart; i <= intFinish; i++) {
main.valueTable.put("index", new Double(i));
( (COldRace) oldRaces.get(i)).FillValueTable(main);
curValue += main.evaluate();
}
return curValue;
}
}
Last edited by sjalle; 07-18-2005 at 04:27 AM.
eschew obfuscation
-
I get what you're saying, and it works, but its not exactly what I want here. I think its overcomplicated because I don't think I need multiple inheritance.
But basically I got my problem simplfied I think. Here's how to reproduce it. In NetBeans, create two classes.
The first class goes like this...
Code:
public class MainClass1 {
public class Subclass1{
}
}
Then create another class like this...
Code:
public class MainClass2 {
public class Subclass2 extends MainClass1.Subclass1
}
}
The declaration of Subclass2 will be underlined in red (error) and say "an enclosing instance that contains MainClass1.Subclass1 is required"
So what I doing wrong. Is this even possible?
Last edited by phatslug311; 07-18-2005 at 05:44 PM.
-
Nevermind, I think I am talking about multiple inheritance, and your idea with an interface works just fine, so I think I'll use that.
Thanks for the help!
-
The solution ...
The problem is that Subclass1 is actually tied to an instance of a MainClass1 object - for example, it could be accessing data belonging to a specific instance of MainClass1.
So, you can't sub-class it without indicating which instance of MainClass1 you are sub-classing it for.
Anyways, if you make public class Subclass1 static, then the problem goes away (assuming that's what you want, that Subclass1 isn't really accessing non-static data belonging to MainClass1.
So, the fix is this:
public class MainClass1 {
static public class Subclass1{
}
}
public class MainClass2 {
public class Subclass2 extends MainClass1.Subclass1
}
}
hope that helps.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|