aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Inner class method calls Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Anonymous Inner class method calls" Watch "Anonymous Inner class method calls" New topic
Author

Anonymous Inner class method calls

Natalie Ap
Ranch Hand

Joined: Jan 09, 2009
Posts: 49


Hi all,

In order to understand method calls to an anonymous inner class, I wrote the above code. After running it, the following output is generated:

In Test:method()
In Anonymous class: method()

Then, I tried renaming 'method 2' from method() to methodX(). After that, I noticed that 'method 1' started getting called and the output was:
In Test:method()
In Test:method()
In Test:method() until a StackOverflow occured.

How does 'method-call 1' really work. Why doesn't a compiler error occur at the call stating that there is no such method defined in the anonymous class..

Thanks


Fiona
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Because the reference type is Test, the compiler does not give error.
The compile know that, even if the anonymous class does not overide the method it can atleast call the Test class method.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


This is anonymous inner class, that is extending Test class, means it is subclass of Test class, so it inherits method() from Test class.


SCJP 6
Natalie Ap
Ranch Hand

Joined: Jan 09, 2009
Posts: 49
Punit Singh wrote:

This is anonymous inner class, that is extending Test class, means it is subclass of Test class, so it inherits method() from Test class.


Thanks..that explains..I do not like this topic of anonymous inner classes much and i haven't really been touching it!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Inner class method calls
 
Similar Threads
Regarding inner classes
anonymous class?
TreeMap containsKey issue
Anonymous inner class
Is there a way to instantiate anonymous inner class?