My doubt is: ------------- here start() is inherited from the threadclass.It will access only the methods and variables of it's class.Here how this start() method calls the subclass MyThread's run() method. I think it calls only the superclass run method();
No, it will call the run method that is defined in class as far down in the class hierarchy of the current object as possible. So if your MyThread subclass overrides the run method, that one will be called. That's the whole purpose of polymorphism.
This behavior actually isn't specific to threads at all - moving to Java in General...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
The start() in java.lang.Thread calls a native method start0(), which sets up requisite datastructure for thread. Once native method is done with what it has to do, it calls the run() in java.lang.Thread. java.lang.Thread.run() sees if the subclass of Thread overrides run() and executes the overriden method.
Joined: Jul 11, 2001
Originally posted by frens java:
java.lang.Thread.run() sees if the subclass of Thread overrides run() and executes the overriden method.
Actually it's not Thread.run() which looks for it, but the JVM which looks for whether the method is overridden. Thread.run() actually does nothing at all (take a look at its source code that comes with the JDK).