Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Anonyms Inner Class ????

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Folk's,

I would like to know how to call the anonyms innter class and the below example how can i call the method "sizzle". Pls explain to me.

Thanks, Raghu.K




[ September 13, 2006: Message edited by: Barry Gaunt ]
[ September 14, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You won't be able to call it with the p reference. Since sizzle() is not defined in Popcorn, you can't call it on an instance of the anonymous inner class.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't.

Note that the only way to access an "anonymous inner class" is through the reference of the super class (Popcorn in the current case.) Since Popcorn class doesnot know "sizzle" method, there is no way you can call sizzle method on the anonymous inner class just created.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Raghu, [B]please use tags around properly formatted code,[/B] it makes youe example so much easier to read and understand.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hope this answers your question!!!


Note:
Please do not tell you are getting an error like the one below
>>>Food.java:23: cannot resolve symbol
symbol : class Food$1
location: class Food
perchance you meant 'Food.1'
((Food$1)p).sizzle();
^
1 error
> Terminated with exit code 1.

---
I am sure my code is correct will work.
Well! if you found the answer share it in the forum.

Happy Homeworking
[ September 14, 2006: Message edited by: febians sh ]
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

First overridding means, you are defining a method which is of same signature of your parent method. In your Anon. Class the sizzle() is not an overriding method and you are assiging it to Pop type, so you can't access it.

Remember:

1) Reference type super class can't access, new methods of its subclasses.
2) Reference type attributs always referes to Class type of reference.
3) Reference type methods alwyas referes to Reference type class even type is of Super Class.

I know they are confusing, but just read three times.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"febians sh", please read our JavaRanch Naming Policy and change your displayed name to comply with it. The correct format for a displayed name is <first name or initials for first names><space><family name> and we prefer it if you use your real names.

Thanks
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by febians sh:




Your first solution is a hack - you should not require a successful compilation without the hack so as to get the hack to compile. Also it goes against Sun's recommendations to use knowlege of anonymous classes generated by the compiler.

The second solution using Reflection is correct but that is beyond the scope of SCJP.

For SCJP examination purposes the answer is "you cannot call sizzle() through the reference to the anonymous class".
[ September 14, 2006: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could invoke the sizzle method like this, but not much of use though.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a good catch, but it works only once because the new object goes poof after the call (I suppose it really gets sizzled in that case).
 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Folk's

Thanks for all your reply I understood the concept.

Barry Gaunt,
sure, I will format the code next time.

Regard's Raghu.K
 
Come have lunch with me Arthur. Adventure will follow. This tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic