File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exam Prep Q's Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exam Prep Q Watch "Exam Prep Q New topic
Author

Exam Prep Q's

K Ville
Ranch Hand

Joined: Aug 16, 2003
Posts: 58
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!


Thinking is the talking of the soul with itself...-Plato
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
K Ville
Ranch Hand

Joined: Aug 16, 2003
Posts: 58
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

Joined: Feb 27, 2002
Posts: 8919

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.


Groovy
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
They can also declare new methods.
K Ville
Ranch Hand

Joined: Aug 16, 2003
Posts: 58
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

Joined: Mar 20, 2001
Posts: 210

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exam Prep Q's