aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes kb6 self test question and general doubt regarding when overriding rules apply? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "kb6 self test question and general doubt regarding when overriding rules apply?" Watch "kb6 self test question and general doubt regarding when overriding rules apply?" New topic
Author

kb6 self test question and general doubt regarding when overriding rules apply?

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Please consider the following program from kb6 self test questions page no. 781-782.



When line A(shown in comment) viz. new Thread(new Starter()).run(); is called shouldn't it call the run method of java.lang.Thread class ? the output of the program suggests the overridden run() method of starter class is called. but why it happens this way ? since we are calling run on java.lang.Thread object why it calls overridden run method.

also i think the reason may be relating to my earlier post http://www.coderanch.com/t/579781/java-programmer-SCJP/certification/object-initialization. in the given post Henry says that overriding rules apply even before object initialization. would like somebody to elaborate on this OVERRIDING RULES APPLY BEFORE OBJECT INITIALIZATION. or if i generalize WHEN OVERRIDING RULES ARE APPLIED ?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

In this example the run method of Thread class will be called which will call the run method of Starter class. The code is not overriding run method of Thread class as the Starter class is not extending the Thread class...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

thanks for the reply Ankit. i'm not able to understand however the reason for the question at http://www.coderanch.com/t/579781/java-programmer-...fication/object-initialization.. i read an article on object initialization by bill verners at javaworld.com http://www.javaworld.com/javaworld/jw-03-1998/jw-03-initialization.html?page=4. in the conclusion part authors says that if the superclass constructor calls a method, that is overridden in the subclass, then when we create object of subclass , overridden method of subclass will run (when the superclass constructor is invoked). how come overriding comes into play even before object initialization. what is the reason for this ?
Panagiotis Kalogeropoulos
Rancher

Joined: May 27, 2011
Posts: 99

When line A(shown in comment) viz. new Thread(new Starter()).run(); is called shouldn't it call the run method of java.lang.Thread class ? the output of the program suggests the overridden run() method of starter class is called. but why it happens this way ? since we are calling run on java.lang.Thread object why it calls overridden run method.


Your Starter class is a Runnable (implements the Runnable interface) and not a Thread (does not extend the Thread class). So when you want to create a new Thread by calling



you are passing a Runnable to the Thread constructor (http://docs.oracle.com/javase/6/docs/api/java/lang/Thread.html#Thread(java.lang.Runnable)). And if you look at the API of the Thread class regarding the run() method, you will see it says:

If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.


So this is why your run() method is executed. Important note: by calling run() you do not start a new thread and the code will be executed sequentially and not concurrently. In order to start a new thread you must invoke the start() method. Try change run() with start() and you will see two different numbers in the print out (two different threads with different Ids).


how come overriding comes into play even before object initialization. what is the reason for this ?


Lets see the following senario:

Suppose you have an abstract class like this:



As you know, we can't instantiate abstract classes. So we must create a subclass that we can use.



So if you try to instantiate the ConcreteClass like this



you will see the following output

doSomething1 is called
doSomething2 is called

As you can see, we have overriden the abstract methods. These methods are executed inside the AbstractClass's constructor and we know that an object is not instantiated until its constructors have returned (meaning they have finished their job). So as you can see the ConcreteClass is not instantiated up until its constructor and the super constructor have finished. But inside the super constructor are two methods that we must override in order to execute properly our code.

So as you can see, overriden methods can be called even before our objects are fully initialized. The above code in fact gives the opportunity to subclasses to apply their own logic during the object's initialization by overriding the abstract methods.

I hope all the above helped you clarify a little bit more this issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: kb6 self test question and general doubt regarding when overriding rules apply?