• Post Reply Bookmark Topic Watch Topic
  • New Topic

choose between 2 run methods

 
Nada Mohammed
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,
i have the follwoing example :
class A implements Runnable {
A() {new Thread(this).start() ;}
public void run() {
~~~~~~~
}//run
}//A
class extendA extends A {

extendA() {new Thread(this).start() ; }
public viod run() {
~~~~~~~
}//run
}//extendA
my problem is the follwoing :
extendA a=new extendA() ;
object a has two threads one is define inside its class and one is
inherited from parent .The problem araise when i run program both
threads excute run which is inside class extendA .
what i need is :
thread inside class extendA excute run which is in the same
class & thread inside class A perform run which is inside its class

Thank you very much
>< Nada ><

 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor of the derived class will cause the default constructor of the super class be called unless one of the super class constructors is called explicitly
for example

The program above will call the constructor of b as well as the default constructor of a printing out "default constructor of a".
If you however do not want to invoke the default super class consturctor then you can do so by invoking one of the super class constructors in the first line of the constructor of the derived class like this.
b(){
super("Super class overloaded constructor called");
}

Why i told you this is that if you want to create only one thread in your derived class then you must define your classes like this

This will create only one thread when you execute
extendA a=new extendA() ;

------------------
Regds.
Rahul P. Mahindrakar


[This message has been edited by Rahul Mahindrakar (edited September 29, 2000).]
 
Jerry Pulley
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nada;
If I get the gist of your post, you're trying to create an inheritance structure such that when you create an object of the subclass, every constructor up the superclass chain launches a thread with its own version of <code>run()</code>. Way cool! I'm going to find a place to use that, as soon as I figure out how to make it happen.
The reason for the behavior you observed (the two threads both use the subclass version of <code>run()</code>) is that the subclass version overrides the superclass version. You're creating an object of the subclass, and method resolution depends on the actual class of the object created.
Let's see...
<pre><code>
class A {
A() {
new Thread( new Runnable() {
public void run() {
System.out.println( "Superclass thread" );
}
}).start();
}
}
class ExtendA extends A {
ExtendA() {
new Thread( new Runnable() {
public void run() {
System.out.println( "Subclass thread" );
}
}).start();
}
}
public class NeatIdea {
public static void main( String[] args ) {
ExtendA extA = new ExtendA();
}
}
</code></pre>
That'll do what you want, but now that you know what's wrong you can probably do it without the inner class baggage. The trick is to somehow move the <code>run()</code> methods out of the classes in the inheritance chain, so they're not subject to overriding.
Of course, I could just be on a caffeine binge and totally misreading your post. Let me know if this is what you're after.
jply

[This message has been edited by Jerry Pulley (edited October 02, 2000).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!