views:

95

answers:

5

I'm trying to use extends (inheritance) in Java. I made a quick abstract class to extend from, and then extended it. However my IDE now is saying that "An enclosing instance that contains abstract_class is required" and gives my constructor for the derived classes big error lines. What on earth is it going on about? The abstract class doesn't have or need any sort of constructor.

Just for reference, I'm using extends rather than implements in part because the implementation details that I don't want to have to maintain for every derived class which are identical involve using reflection on this.

Edit: I've read some of the responses. What in God's name is a static (or non-static, for that matter) class? And just to irritate all of you, it didn't solve the problem.

// some_class.java
public class some_class {
    public static abstract class abstract_class {
        ...
    }
    ...
}

// Model.java
public class Model extends some_class.abstract_class {
    public Model(...) {
        // No enclosing instance! Critical error.
        ...
    }
    ...
}

And I thought that C++'s header files were bad.

A: 

The "enclosing instance" message almost certainly implies that you have a (non-static) inner class for your superclass. In most cases, inner classes can and should be static - that's likely the best workaround here. Alternatively, as the message says, you will need to use an enclosing instance of the "outer" class, if your parent really makes sense as a non-static inner class.

Posting some code will help disambiguate between these causes and suggest the best way to resolve it. I'll also be able to give examples of the resolutions with the right class names - currently I don't think arbitrary names will help that much as it sounds like you hadn't identified the inner/outer class issue.

Andrzej Doyle
A: 

Do not have classes inside other classes! (yet :) )

Thorbjørn Ravn Andersen
A: 

You need to in your child class add in the constructor super() that super class can be created.

Vash
+1  A: 

if you have

interface MyInterface
{

   abstract class MyAbstractClass {
     // ...
   }
}

and then you try

class ConcreteClass extends MyAbstractClass {

}

You will get the error described. The fix is to either move MyAbstractClass to a top-level class (put it in it's own file - not strictly necessary for non-public classes, but keeps the code organized.) Alternatively, add the static modifier to the MyAbstractClass declaration.

mdma
+1  A: 

The code you posted seems to compile just fine for me. Try doing a clean build in your IDE and it should work.

Just for your own curiosity, Java has 2 types of inner classes: static and regular or (non-static). If you don't include the static keyword for an inner class definition, it means that an instance of that class will always require an instance of the parent class. For ex:

public class MyClassOuter {
    //...
    public class MyClassInner {
        //..
    }
}

If you write that, it is understood that any instance of MyClassInner will have an implicit reference to an instance of MyClassOuter.

Static, on the other, hand implies no such thing. It is just a class definition that happens to be inside another class definition. The outer class is used almost like a package (though not quite).

Java Drinker