• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ch8 K&B 1.4 qno:8

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following,
public class Foo {
Foo() {System.out.print("foo");}
class Bar{
Bar() {System.out.print("bar");}
public void go() {System.out.print("hi");}
}
public static void main (String [] args) {
Foo f = new Foo();
f.makeBar();
}
void makeBar() {
(new Bar() {}).go();
}
}
what is the result?
A. Compilation fails.
B. An error occurs at runtime.
C. foobarhi
D. barhi
E. hi
answer
-------
8. C is correct because first the Foo instance is created, which means the Foo constructor runs and prints �foo�. Next, the makeBar() method is invoked which creates a Bar
,which means the Bar constructor runs and prints �bar�, and finally the go() method is invoked on the new Bar instance
, which means the go() method prints �hi�.
A, C, D, E, and F are incorrect based on the program logic described above.
what my doubt is in method makeBar(){
(new Bar(){}/annoynomous class).go();
but bert is saying creates Bar and calls go method on Bar instace in the explanation .what i thing is the created object is subtype of Bar since annoymous class is invovled.please tell whether i am right or not
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nagendra-

The code below seems to indicate that the class is indeed a Bar and not a subclass of Bar.



Output:


foobarBlah
Bar
I'm a Bar



You can see that I've added some toString() methods there to make it a little clearer. If you comment out the toString() method for Bar, you'll see that it uses the Object type's toString().

I've also overriden the go method and added another method. It seems weird to be able to do that to Bar since I'm not subclassing it, but maybe an anonymous class is a bit different in this respect.

It's an excellent question. Maybe other folks can shed some light on it.

Josh
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nagendra-

After looking at it a little more and doing some reading in Kathy and Berts latest book, I think you're right. I think that the class created in the makeBar() method is a subclass of Bar. The results still make sense becaues a subclass will inherit the public method go() of Bar and the public toString() method in my modified code. The syntax "new SomeClass() {" with a curly brace indicates that you're either extending a class or implementing an interface with an anonymous class. There's no way it's simply a Bar. It's got to be a subclass.

Does the text that you're reading explicitly say that it's an instance of Bar and opposed to an instance of a subclass of Bar?

Does anyone else want to weigh in on this one?

Thanks,
Josh
 
nagendra satyakrishna jasti
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah it is stating explicitly Bar you can see confusion icons in the explanation of the question
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nagendra-

I think you miscopied the explanation from the book. Here is what it says in mine.


C is correct because first the Foo instance is created, which means the Foo constructor runs and prints foo. Next, the makeBar() method is invoked, which create a Bar, which means the Bar constructor runs and prints bar, and finally an instance is created (of an anonymous subtype of Bar), from which the go() method is invoked. Note that the line (new Bar() {}).go(); creates a little tiny anonymous inner class, a subtype of Bar.



Josh
 
nagendra satyakrishna jasti
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
mine is dreamtech publisher and it is an indian copy i copied exactly from the
book(cut and paste from electronic book) i think it is not printed properly
thanks for your response and pasting that content
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nagendra-

No problem.

The difference between the two texts is pretty severe. I would contact the publisher if I were you and see if there are other differences that you need to be aware of.

Good luck,
Josh
reply
    Bookmark Topic Watch Topic
  • New Topic