• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there a way to instantiate anonymous inner class?

 
Bhaarat Sharma
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Is there a way to call pop_test method by instantiating the anonymous inner class
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Are you sure this is correct snippet of code. Please quote the source of the code snippet above.






Best Regards,
 
Bhaarat Sharma
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry. was a typo on my part. I've corrected it.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Actually, it won't be able to invoke the pop_test() method i suppose.

If you see this part of code.


It says that inside class Test, declare a reference variable of type Annonymous, then declare a new class that has no name but is the
subclass of Anonymous. The whole point to declare such classes is to override the methods of the super-class that is Anonymous.
It should be noted that you are using a super-class reference to refer to the sub-class object. There are potential limitations to this.
You can only call those methods on an anonymous inner class that are defined the reference variable type.

Spot the problem here, in this anonymous class, rather then overriding the method you are declaring a new method called pop_test,
but the reference type is of Super class and super class won't know anything about this method. That is a potential limitation in this case.

Hope this helps,



 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have amended the code a bit. Check it, it won't see any method like pop_test() as the variable declared is of Super class.

Hope this helps,
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhaarat Sharma wrote:sorry. was a typo on my part. I've corrected it.


Please quote the sources from where you copied this code. It is the policy.

Best Regards,
 
Bhaarat Sharma
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that makes sense.

Source is the my modified version of K&B objective: Chapter 8 Inner Classes
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome mate.

Happy Preparation
 
Larry Olson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't clear to me where exactly you intend to call the pop_test method. But you can call it like this:



Just call the method by adding ".pop_test()" to the end of the anonymous class declaration. In this case you can't really assign the new Anonymous class to a reference since the pop_test method returns void. You are just instantiating it and calling the pop_test() method along with it. All in one if that is what you want.
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The class declaration is no longer in scope after the instance is created, so if you want to store the reference *and* call the method, you will need to use reflection to call the method.

Henry

 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks Henry for sharing such a good insight.

Yes Larry has pointed out well, what was the reason you wanted to call. I replied to you in context because
you wanted to invoke the method via reference. What specific need it can have?

Best Regards,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic