| Author |
method overriding
|
janet hu
Greenhorn
Joined: Dec 26, 2005
Posts: 2
|
|
A question from mock exam: JavaCertificate.com What is the output of the following program when you compile and execute it? Assume classes are defined in the same package. 1. class Phone { 2. static String device = "Phone.device"; 3. void showDevice() { 4. System.out.print("Phone.showDevice," + device + " "); 5. } 6. Phone() { 7. showDevice(); 8. } 9. } 1. class Mobile extends Phone { 2. String device = "Mobile.device"; 3. void showDevice() { 4. System.out.print("Mobile.showDevice," + device + " "); 5. } 6. Mobile() { 7. showDevice(); 8. } 9. public static void main(String[] args) { 10. Mobile n = new Mobile(); 11. n.showDevice(); 12. } 13. } 1 Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device 2 Phone.showDevice,Phone.device Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device 3 Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device 4 Compile Time error 5 RunTimeException is thrown correct answer is 3, why? my answer is 2. [ December 26, 2005: Message edited by: janet hu ] [ December 26, 2005: Message edited by: janet hu ]
|
 |
Thomas De Vos
stable boy
Ranch Hand
Joined: Apr 12, 2003
Posts: 425
|
|
For the completeness of the question, this is the feedback at our website: - A new instance is created of the Mobile class, this class extends the Phone class. When the instance is created the Phone constructor is called, this constructor calls the showDevice() method, the showDevice() method of the Mobile class is called, this method is overridden in the Mobile class. This prints out "Mobile.showDevice,null", at the moment the showDevice() method is called the instance variables of the Mobile class are not yet initialized, the device variable is "null". After this the constructor of the Mobile class is called, once again the showDevice() method of the Mobile class is called. At this moment the device variable is initialized and will print out: Mobile.showDevice,Mobile.device. The main method calls the showDevice() method of the Mobile class and prints out: "Mobile.showDevice,Mobile.device". The complete output is: "Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device" -
|
Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
|
 |
Niranjan Deshpande
Ranch Hand
Joined: Oct 16, 2005
Posts: 1277
|
|
hi janet this code is from javacertificate.com...isnt it the explanation is give in the website itself...its hard to digest but as u mature after a lots of study, the explanation will finally become clearer niranjan
|
SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
|
 |
janet hu
Greenhorn
Joined: Dec 26, 2005
Posts: 2
|
|
Originally posted by Thomas De Vos: For the completeness of the question, this is the feedback at our website: - A new instance is created of the Mobile class, this class extends the Phone class. When the instance is created the Phone constructor is called, this constructor "of phone class??" calls the showDevice() method, the showDevice() method of the "phone??" class is called, this method is overridden in the Mobile class. The complete output is: "Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device" -
|
 |
 |
|
|
subject: method overriding
|
|
|