26

Consider the below code:

class abstract Normal1 extends Something 
{
}

class Outer 
{
  class abstract Inner extends Normal1
  {

  }
}

class General extends Outer.Inner  // Problem occurs at this 
{
}

The error I am getting is "No enclosing instance of type PerfHelper is available due to some intermediate constructor invocation"

My question is can I extend the inner class like above ?

user1803551
  • 12,965
  • 5
  • 47
  • 74
user911333
  • 261
  • 1
  • 3
  • 4
  • Apart from the answer to your question (inner classes are tied to an enclosing instance of their outer class unless declared static), why would you want to do something this convoluted? Trying to extend an inner class in an unrelated context is probably a sign that you should break them out into their own classes. – Steve B. Nov 18 '11 at 04:39
  • Different Scenario


    class abstract normal1 extends something { } class outer extends normal1 <== change1 { class abstract inner extends outer <==change 2 { } } class General extends outer.inner <== Problem occurs at this { }

    what is the solution for this
    – user911333 Nov 18 '11 at 06:23
  • 1
    Where is `PerfHelper` being used in your example? – Eric Feb 02 '13 at 21:50

4 Answers4

35

Declare the inner class as static and you should be able to extend it:

class outer {
  static abstract class inner extends normal1 { }
}

If inner is not abstract, it's tied to outer, and can only exist when an instance of outer exists. Check to see if this is what you really want.

Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
Jordão
  • 55,340
  • 13
  • 112
  • 144
6

Nested class are like(in sense ) Property of a class.

  1. As in case of instance variable it only when available when its object is created as same as inner class also available when outer's object will created.

So if you want to extend this then make your inner class as static inner class
As jordao suggest above

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
2

In your class General modify its constructor a to call super inner class constructor. here is the code..

public General(){
    new outer().super();
}
Siddappa Walake
  • 303
  • 5
  • 14
2

Try this, (Read nested class inheritance rules).

abstract class normal1 extends something { }

class outer
{
  abstract class  inner extends normal1{}
}

class Outer1 extends outer
{
  class General extends inner {}
}
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186