aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonym. inner class extending its enclosing class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonym. inner class extending its enclosing class" Watch "Anonym. inner class extending its enclosing class" New topic
Author

Anonym. inner class extending its enclosing class

Martin Podrouzek
Greenhorn

Joined: Sep 09, 2004
Posts: 10
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

Joined: Jul 19, 2004
Posts: 298
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

Joined: Sep 09, 2004
Posts: 10
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

Joined: Dec 09, 2000
Posts: 214
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.[


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
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

Joined: Jul 19, 2004
Posts: 298
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
 
 
subject: Anonym. inner class extending its enclosing class