I have multiple levels of inner classes. The extend keyword works for the first sub-level, but not the second. Meaning:
public class A{
.........
public class B extends A{
..........
public class C extends B{
........
}
}
}
This gives me an error for public class C extends B{
. It says:
No enclosing instance of type A is available due to some intermediate constructor invocation.
Why am I getting this error and how can I fix it? I thought the default constructors would deal with this.
Your code is legal and should work. The fact you've got the error means one of:
I looked at your error that states
No enclosing instance of type A is available due to some intermediate constructor invocation.
So I suppose there are some constructors declared in your code. Possible reason of your failure is you declared constructor with parameters for class A
but haven't done that for B
and C
. Explanation from spec:
It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor that takes no arguments and has no throws clause.
Another possibility is that you already have A
or B
class in your package or you have import
ed A
or B
class from another package.
The error can be misleading if compiler doesn't know what class do you extend.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With