• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Another anoymous question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
54. which two are true?
A. An anonymous inner class can be declared inside of a method
B. An anonymous inner class constructor can take arguments in some situations
C. An anonymous inner class that is a direct subclass of Object can implements
multiple interface
D. Even if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements a single interface
E. Even if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements multipe interface
Answer is A.D.
I don't think D is correct. Anoymous class can either extend a class or implement one interface, can not do both. And it says here Super does not implement any interfaces, so no inheritance here. Maybe superclass of Super implements a interface?? What do u think?
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ase, The instanceof operator returns true for classes all the way up the heirarchy. So my understanding would be that it is possible for an anonymous class to implement an interface even if the immediate super does not specifically.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct answer is A and B.
Anonymous classes have constructors, just as ALL objects have a constructor. You just can't declare an explicit constructor.
See JSL section 15.9.5.1 Anonymous Constructors:


An anonymous class cannot have an explicitly declared constructor. Instead, the compiler must automatically provide an anonymous constructor for the anonymous class. The form of the anonymous constructor of an anonymous class C with direct superclass S is as follows:
If S is not an inner class, or if S is a local class that occurs in a static context, then the anonymous constructor has one formal parameter for each actual argument to the class instance creation expression in which C is declared. The actual arguments to the class instance creation expression are used to determine a constructor cs of S, using the same rules as for method invocations (�15.12). The type of each formal parameter of the anonymous constructor must be identical to the corresponding formal parameter of cs. The body of the constructor consists of an explicit constructor invocation (�8.8.5.1) of the form super(...), where the actual arguments are the formal parameters of the constructor, in the order they were declared.
Otherwise, the first formal parameter of the constructor of C represents the value of the immediately enclosing instance of i with respect to S. The type of this parameter is the class type that immediately encloses the declaration of S. The constructor has an additional formal parameter for each actual argument to the class instance creation expression that declared the anonymous class. The nth formal parameter e corresponds to the n - 1st actual argument. The actual arguments to the class instance creation expression are used to determine a constructor cs of S, using the same rules as for method invocations (�15.12). The type of each formal parameter of the anonymous constructor must be identical to the corresponding formal parameter of cs. The body of the constructor consists of an explicit constructor invocation (�8.8.5.1) of the form o.super(...), where o is the first formal parameter of the constructor, and the actual arguments are the subsequent formal parameters of the constructor, in the order they were declared.
In all cases, the throws clause of an anonymous constructor must list all the checked exceptions thrown by the explicit superclass constructor invocation statement contained within the anonymous constructor, and all checked exceptions thrown by any instance initializers or instance variable initializers of the anonymous class.
Note that it is possible for the signature of the anonymous constructor to refer to an inaccessible type (for example, if such a type occurred in the signature of the superclass constructor cs). This does not, in itself, cause any errors at either compile time or run time.



D. Even if a class Super does not implement any interfaces, it is still possible to define an anonymous inner class that is an immediate subclass of Super that implements a single interface


This statement is false. When you declare an anonymous class, you can specify either the Superclass of the anonymous class, OR an interface that the anonymous class will implmenent. If you choose the interface, then the Superclass of the anonymous class is just java.lang.Object, and it implements the supplied interface.
 
Ase Lindskog
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, thank you for your reply.It definately take a while to read, but, yes, I agree it should be A&B.
 
Mike Kelly
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rob.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic