• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonym. inner class extending its enclosing class

 
Martin Podrouzek
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

I was experimenting with code while preparing for the exam and I came across a problem I couldn't solve. I want to create an anonymous inner class that extends its enclosing class (its parent). I was thinking of something like this (please note that code will compile with javac, but fails under Eclipse):


I would also like to polymorph the anonymous class to its parent. I'm not sure if the code is ok, since I wasn't able to run it. I believe that the code would yield "Anonymous Inner class eating", but I'm not sure. Do you know what's going on "under the hood"?

Any hints, advices are greatly appreciated.

Thanks

Martin
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah u r right, its printing "Anonymous Inner class eating..."
So whats the problem u having and also it runs very well in eclipse.
By the way whats the error its displaying in eclipse
 
Martin Podrouzek
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Eclipse displays:

No enclosing instance of type OuterClass is available due to some intermediate constructor invocation

I'm using Eclipse 3.0 with JDK 1.4.2_06.

By the way, do you experience any differences when you use InnerClass instead of OuterClass ?
InnerClass inClass = new OuterClass().new InnerClass() { ... };
[ March 29, 2005: Message edited by: Martin Podrouzek ]
 
Simon Cockayne
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Martin,

Your code fails in my eclipse installation as well.

I am not sure why it fails...but...

If you don't have InnerClass extend its parent AND define inClass as a OuterClass.InnerClass reference it works fine, as shown below.

I am trying to wrap my head around what the inner will give you by extending the outer??? After all the inner class already has access to all the methods and members of the outer class.


CODE] public class OuterClass {
public static void main(String[] args) {

//
OuterClass.InnerClass inClass = new OuterClass().new InnerClass() { // some
// code
// goes
// here...
public void eat() {
System.out.println("Anonymous Inner class eating...");
}
};
inClass.eat();

}

public void eat() {
System.out.println("Outer Class eating...");
}

//class InnerClass extends OuterClass {
class InnerClass {
public void eat() {
System.out.println("Inner class eating...");
}
}
}[/CODE]

Cheers,

Si.[
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i am having jdk 1.3 here, so may be thats the reason.

The error

No enclosing instance of type OuterClass is available due to some intermediate constructor invocation

appears when u r extending the inner class in some other class not under the scope of the outer class. Let me give u an example:


Add this , u will find the above mentioned error
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i dont understand why the code should fail.

public static void main(String[] args) {
OuterClass inClass = new OuterClass().new InnerClass() {
// some code goes here...
public void eat(){
System.out.println("Anonymous Inner class eating...");
}
};
inClass.eat();
}

In the above part of the code, u are invoking new InnerClass() on the instance of OuterClass, so thats perfectly right.
2)

class InnerClass extends OuterClass {

public void eat() {
System.out.println("Inner class eating...");
}
}

Here also the class InnerClass is defined inside the OuterClass which means u cannot instantiate the InnerClass wihtout having reference of OuterClass.

But if the InnerClass had been outside the scope of the class, as i have shown in my previous example, a compiler error would have been displayed.
Well, i guess u know the reason behind it.

Thanks
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic