• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exam Prep Q's

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1: Can anonymous inner classes declare their own variables? If so, what modifiers can we give them?
Q2: Can we define constructors for our abstract class and invoke them via call to super() from either a child class constructor or an anonymous subclass?
Thanks!Taking the exam on the 21st of this month! Help!
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
q1. Do you mean declaring fields that did not exist on the type an anonymous class extends or implements? I don't think so. An anonymous class does not declares a new type, now, how the new members would be accessed without a proper type?
q2. Yes I think is OK. It is not possible to create an instance of an abstract class, but the initialization in its constructor may be applied to subtype instances.
Notice:
The answers could have been extracted by running examples.
I have no a compiler at the moment.
 
K Ville
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, sir. I thought it would be legal for an anonymous class to declare a member variable of its own. But does it inherit its superclass' or interface's variables instead?
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Q1: Can anonymous inner classes declare their own variables? If so, what modifiers can we give them?


Anonymous class can declare variables. Here is an example.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They can also declare new methods.
 
K Ville
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a follow-up question to Mr. Botella: Sir, if an anonymous inner class can actually declare its own methods, why does the K&B book say it will generate a compiler error if you invoke a method not defined in your superclass or interface? It is also stated there that the main purpose of defining an inner class is overriding?
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by K Ville:
As a follow-up question to Mr. Botella: Sir, if an anonymous inner class can actually declare its own methods, why does the K&B book say it will generate a compiler error if you invoke a method not defined in your superclass or interface? It is also stated there that the main purpose of defining an inner class is overriding?


It probably trying to say that you can not invoke those method from outside of the anonymous inner class. The reason is with anonymous class you basically lock(sign the contract) that say your anonymous object IS a specific class or interface, where the compiler will only recognized the method that were declared in those class or interface.
It's not much different when you are dealing with non-anonymous class,
when you have an instance of implementation object being define as its interface..you can't use any specific method not in the interface unless you
manually cast it back. For example

Hope that help
 
reply
    Bookmark Topic Watch Topic
  • New Topic