aspose file tools*
The moose likes Beginning Java and the fly likes Inner class and interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner class and interface" Watch "Inner class and interface" New topic
Author

Inner class and interface

Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi
See the code below.
I unable to compile, if I uncomment the line "// t2.testA();".
Here my understaning is, it gives error, because,
the method "testA()" is not declared in the interface Orange.
Which makes sense. If that is the case, why java allows us to
define method "testA" at the time I implement interface Oracle
in class "Test"
In other words, what is the purpose of method "testA",
when I am unable to use it.
Hope I make sense.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If you add methods to an anonymous class this way, it's true that you can't call then from other code. But you could call them from within the anonymous class itself, and that's why Java must allow you to define new methods in an anonymous class.
By using reflection, you actually could call that method from outside of the anonymous class: you could ask the instance for its Class object with getClass(), then get a list of Method objects with getDeclaredMethods(), and then call the "orphaned" method using the appropriate Method object.


[Jess in Action][AskingGoodQuestions]
Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Ernest,
Thank you.
BTW, don't you think that this is violating the access specifier rule.
Given the fact that, the instance is fully available to the main method.
thanks
siva
Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi Ernest,
Thanks for your input. I think I got it.
Type Orange don't have method testA, but Orange2 does have method testA defined in it.
thanks
siva
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class and interface