• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Anonymous classes doubt

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can we overload constructors in anonymous classes? Also can we override the default no-arg constructors? If yes, pls explain with an example.
Is there any way I can invoke sizzle() on an anonymous subclass of Popcorn?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using an anonymous class instance, you can only call methods that the
parent class or implemented interface is having. Just remember, polymorphism.
You can only use anonymous class specific methods inside the class only,
but not outside using the reference variable.



Thanks,
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can i use overloaded constructor in anonymous class?
No, as the anonymous class shares the superclass (popcorn) and anonymous classes have no name it is not possible.

can i use default constructor in anonymous class ?
No, only the main class constructor is used.

Why cant i invoke the sizzle method ?

Here you are getting a compilation error
popcorn p=new popcorn();
p.sizzle();

as the compiler checks for the reference type first ,
popcorn class does not have this method so it melts down.
(overiding method selection based on object happens at runtime not at compile time)
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apart from the above explainations ..
I guess the only place you can call sizzle() is the pop() method itself!!!
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic