I am just wondering what the result would be if I subclassed a class which extends Thread and I wrote following code and tested:
class A extends Thread {
public A() {
this.start();
}
public void run() {
System.out.println(" in A " + Thread.currentThread().getName());
}
}
class B extends A {
public void run() {
System.out.println(" in B " + Thread.currentThread().getName());
}
}
public class OverrideRun {
public static void main(String[] args) {
A a = new A();
B b = new B();
}
}
And the result is:
in A Thread-0
in B Thread-1
But I don't understand why two threads are created?
This is because the B b = new B(); statement calls no argument parameter of class B and that calls no argument parameter constructor(Default) of class A.
This is due to constructor chaining.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With