This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Inner class from K&B mock exam

 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I can understand D but i can't understand C.
How does it create an anonymous inner class.
[ June 23, 2004: Message edited by: Anshul Kayastha ]
 
arnel nicolas
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont have the K&B book but basing from the code
i have the following doubtful explanation.Hope you dont mind.

1) for Letter A = you cannot assign class Bing to class Bang because
class Bing is a superclass. It will cause a runtime error.

2) for Letter B = the class Bing does not have a constructor that takes an int parameter.

3) for Letter C = its obviously correct.

4) for Letter D = it creates an anonymous class of type Bang and stores it to
a type of class Bing. It is perfectly legal because Bang is a subclass of class Bing.

cheers,
arnel
 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying
3) for Letter C = its obviously correct.


That is what I wanted to know.
Why is C right?
Is it legal to write soemthing like "Foo". Shouldn't it be a parameter or something.
I have never seen something like

{ //curly brace
"Foo";
}//curly brace ends


What does this code mean?
 
Beny Na
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi..i got an error when try to compile that..

class Bing {
Bing(String s){} Bing() {}}

class Bang extends Bing {
Bang() {}
Bang(String bangS)
{ super(bangS); }
Bing b = new Bing(){ "Foo" }; // this line won't compile

//insert code here }

}


i do not understand why C is correct..and the java compiler show error for that.
 
Chris Allen
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible that answer C should have been originally typed as:


to match the Bing constructor that takes a String?
 
K Anshul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Allen:
Is it possible that answer C should have been originally typed as:


to match the Bing constructor that takes a String?


Might be. I saw the question in K&B mock exam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic