• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

thread and overriding  RSS feed

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any body explain the reason for answer of following code
it gives 1 as output.i can't understand what is really happen

class B extends Thread {
B(){}
B(A a){}
public void run(){
System.out.println("1");
}
}


class A extends B {
A(){}
A(B a){}
public void run(){
System.out.println("2");

}

public static void main( String as[]){

Thread a =new B (new A());

a.start();


}
}
 
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread's "start()" method calls the Thread's "run()" method. The default run() will look to see if the Thread was constructed with a Runnable as an argument; if so, it will call the Runnable's run(), or otherwise, just return.

You've overridden that default behavior to just print "1". The A object actually is not used for anything. start() calls B.run() which prints "1" and exits.

Pretty simple, actually.
 
author
Sheriff
Posts: 23588
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You already asked this question -- and got an answer a while ago...

http://www.coderanch.com/t/417778/java-programmer-SCJP/certification/java-threads

Henry
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24220
40
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:
You already asked this question -- and got an answer a while ago...

http://www.coderanch.com/t/417778/java-programmer-SCJP/certification/java-threads

Henry



Although to be fair, that isn't the right answer!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!