1

For example:

public class A {

public class B extends A {
}

public static void main(String[] args) {
    A a;
    a = new B();
}

}

I searched some similar questions and they showed "yes, a superclass type variable can refer to a subclass object". But in eclipse the above code comes up with an error like "No enclosing instance of type A is accessible. Must qualify the allocation with an enclosing instance of type A (e.g. x.new A() where x is an instance of A)."

So what wrong? Thanks!

Echo
  • 11
  • 1
  • 2
  • Possible duplicate of [Java - No enclosing instance of type Foo is accessible](http://stackoverflow.com/questions/9560600/java-no-enclosing-instance-of-type-foo-is-accessible) – fabian Mar 04 '16 at 00:05

2 Answers2

3

The answer is "yes" superclass can refer to a subclass, but you're asking the wrong question.

You're getting this error because B is an enclosed class of A (meaning you must have an instance of A to have an instance of B), but you're referring to it from a static method (ie not an instance of A).

Simply change B to be a static class.

public class A {

    public static class B extends A { // <-- Added static keyword
    }

    public static void main(String[] args) {
        A a;
        a = new B();
    }
}

No errors.

The other option is leave it an enclosed class and do this:

public class A {

    public class B extends A { // leave B as an enclosed class
    }

    public static void main(String[] args) {
        A a = new A();
        a = a.new B();  // can only create a B in the context of an A
    }
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • @user1429807 Note the alternative, and equally valid, solution to your error. You may find the syntax instructive. – Bohemian Jun 01 '12 at 04:02
  • 1
    Just to confirm, wouldn't this result in a `NullPointerException` as is? I.e. don't you need a non-`null` instance of `A` to instantiate an instance of `B` with? – SimonC Jun 01 '12 at 04:51
  • @SimonC It sure would explode. I edited the question from `A a;` to `A a = new A()`. Thanks for noticing that! – Bohemian Jun 01 '12 at 05:41
-2

check your parantheses!! It should be:

class A {
}
public class B extends A {


public static void main(String[] args) {
    A a;
    a = new B();
}

}

Also you have two public classes in your code!!

Surender Thakran
  • 3,958
  • 11
  • 47
  • 81
  • You have changed the code so that the `main()` is in `B`, but the question is about code in `A`. See my answer for the correct explanation. – Bohemian Jun 01 '12 at 03:55
  • @user1429807: two `public` classes cannot be in the same file. Either you put them in two different files if you want to keep both of them `public` **or** make one of them `default`. – Surender Thakran Jun 01 '12 at 03:56
  • Again... see my answer. All I added was the keyword `static` to his code in one place, and made no other changes, and voila, problem solved. I suggest you actually *read* my answer btw. Your last comment suggests you have not done so, otherwise you wouldn't have made your irrelevant comment about two public classes (which the code in the question does **not** have) – Bohemian Jun 01 '12 at 03:58
  • @Bohemian: yup.. read it and got it!! i mistook it as a problem of parantheses... my bad!! – Surender Thakran Jun 01 '12 at 04:00
  • btw, I added an alternative solution you may be interested in where `B` is left as an enclosed class – Bohemian Jun 01 '12 at 04:05