Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exam Prep Q's

 
K Ville
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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!
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
They can also declare new methods.
 
K Ville
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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?
 
Wirianto Djunaidi
Ranch Hand
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic