• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

anonymous class..

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friends,
I have one doubt please clarify..
Which of the following statements are true?
[a] Inner classes cannot be defined private
[b] An anonymous class can be subclass of another explicit class
[c] An anonymous class can implement single interface
[d] An anonymous class cannot be both explicit subclass and implement an interface
Ans: b,c,d
a,c are clear to me..
please explain b,d.. with example..
2. I am writing Jcp on 22nd.. So I have only 3-4 days to prepare... I have written 5 mock exmas..
Jtips-I got 60%,61%,
Marucs Greens Mock exams.-I got 80%,82%,90%..
Is this preparation is enough..
Can any one give me the URL's of good mock exams like MarcusGreen,which covers only objectivess..
Please give tips for these last 2 days preparation..

thanks in advance..
regards
Suresh
 
Marshal
Posts: 17010
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
suresh,
from RHE, an anonymous inner class is declared in the form:
new Xxxx() { // body };
where Xxxx is an interface, the anonymous class implements this interface and must provide implementations of all methods defined by that interface.
where Xxxx is an explicit class, the anonymous class extends that class and should provide methods that override methods of the explicit class.
given the form of the declaration, you can have either but not both.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[a] Inner classes can be scoped private. You may be looking at an old mock question. Here's a compilable example:

[b] This is a strange question. All classes are subclasses. I'm not sure what distinction 'explicit' makes, but if anonymous classes could only extend Object, they'd even be more miserable creatures than they already are.
[d] is a syntax question. Consider the following class:

This sample demonstrates that you can "subclass" WindowAdapter or "implement" ActionListener. But note how this action occurs "during" the completion of the constructor call. This is ugly syntax; it's simply understood by the compiler that when this construction comes along, the programmer is asking for an anonymous inner class that extends or implements the type being constructed.
The reason you can't both extend and implement for one anonymous class is simply that there's no syntax the compiler understands to support that. BUT, if you write a class that implements an interface and extends a class that you want, you can get around that "limitation" quite easily.

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
suresh seeram
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Junilu and Michel,
Thanks for your fatst replies..
Now it is clear..
thanks and regards,
Suresh
 
Junilu Lacar
Marshal
Posts: 17010
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Ernest:
All classes are subclasses. I'm not sure what distinction 'explicit' makes


I believe "explicit" classes means "explicitly named" classes. That is, an anonymous class cannot be a subclass of another anonymous class.
// not allowed:
Object x = new (new Object() { // body1 } )() { // body2 };
Junilu
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think if the word "explicit" is not there in option D, it will be incorrect since all classes are implecitly subclass of Object.anonymous class canbe both subclass(since implecitly it extends Object) and implement an interface.
correct me if i am wrong.
kinnu
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael:
Just so that we don't confuse anyone, (although I'm about to risk that very thing with this reply), the fact that inner classes can be scoped private is the reason that A is false and therefore not one of the valid answers. This is not an "old" question to which the answer has changed. (Man, I hope that that's clear.)
- Matt Hicks

Originally posted by Michael Ernest:
[B][a] Inner classes can be scoped private. You may be looking at an old mock question. Here's a compilable example:

[/B]


 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, you lost me. What do you mean?
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Matt Hicks
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Michael Ernest:
Sorry, you lost me. What do you mean?


Okay. The original poster's message was this:


Which of the following statements are true?
[a] Inner classes cannot be defined private
[b] An anonymous class can be subclass of another explicit class
[c] An anonymous class can implement single interface
[d] An anonymous class cannot be both explicit subclass and implement an interface
Ans: b,c,d
a,c are clear to me..
please explain b,d.. with example..


Thus, according to this mock exam, answer a is false, answers b,c, & d are true; I believe this is the correct answer.
Your reply:

[a] Inner classes can be scoped private. You may be looking at an old mock question.


seemed to imply that the mock exam had the answer (or the question) wrong, which is not the case. That is what I thought might be confusing, but as I feared, my initial response didn't clear things up. I hope this one has done a better job, but I'm not optimistic.
- Matt Hicks
 
Michael Ernest
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I must not understand what [a] is driving at then, because I was suggesting the statement is incorrect.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic