• 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

Marcus Green MO 1 #34

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
looks MG has changed some Q to thread Q in his Mock1.
ok the foll
Which of the following statements about this code are true?
public class Morecombe{
public static void main(String argv[]){
Morecombe m = new Morecombe();
m.go(new Turing(){});
}
public void go(Turing t){
t.start();
}
}
class Turing extends Thread{
public void run(){
for(int i =0; i < 2; i++){
System.out.println(i);
}
}
}
1) Compilation error due to malformed parameter to go method
2) Compilation error, class Turing has no start method
3) Compilation and output of 0 followed by 1
4) Compilation but runtime error
Answer is 3.
I did not understand how could you pass m.go( new turing(){});
I thought it would be a compile time error but giving an output of 0 1.
Can anybody throw some light on this.
Regards,
madhuri.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madhuri,
You can always pass an anonymous class in place of a named one. The beauty of Java!
The statement:
new Turing() {}
is creating a new Turing object without overridding any methods. It is anonymous because it has not been assigned to anything. It gets assigned to the Turing t parameter when being passed to the go method. Since Turing extends Thread class it is valid to call it's start method.
Regards,
Manfred.
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi manfred,
Thank u for ur post. But I haven't seen such type of passing and still surprised how it works.
Bye,
Madhuri.
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's an anonymous class, as Manfred has pointed out. This is the kind of thing that one can find on the exam, so be careful.
 
madhuri vl
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ceasy,
Thankyou verymuch.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic