Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method overriding

 
janet hu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
-
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
janet hu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
-
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic