• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overridding query

 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Source: http://www.javacertificate.com/

class Phone {
static String device = "Phone.device";
void showDevice() {
System.out.print("Phone.showDevice," + device + " ");
}
Phone() {
showDevice();
}
}
class Mobile extends Phone {
String device = "Mobile.device";
void showDevice() {
System.out.print("Mobile.showDevice," + device + " ");
}
Mobile() {
showDevice();
}
public static void main(String[] args) {
Mobile n = new Mobile();
n.showDevice();
}
}

I do understand how the code works and expected the answer to be
Mobile.showDevice, Mobile.showDevice Mobile.showDevice, Mobile.showDevice Mobile.showDevice, Mobile.showDevice

But the output appears to be
Mobile.showDevice, null Mobile.showDevice, Mobile.showDevice Mobile.showDevice, Mobile.showDevice

I dont understand how String device get its value as null. Is it because the superclass constructor calls the subclass method showDevice() and both classes have the variable with the same name.

Can someone explain me how the value appears to be null.

Thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont understand how String device get its value as null. Is it because the superclass constructor calls the subclass method showDevice() and both classes have the variable with the same name.

Can someone explain me how the value appears to be null.


There are two rules in play here.

One. Inheritance works, even when the object has not completed construction yet. So, the subclass method will be called, even though the construction of the subclass may not be done yet.

Two. There is a particular order to how a constructor is executed...

Step A. The explicit super() constructor, or the implicit no-args super() constructor is called.

Step B. The instance variables are initiated and instance initializers are executed. And it is done in the order the class is defined from the top to the bottom.

Step C. The rest of the constructor is called.

The important take-away, for rule number two, is that the initialization of the variables is done after the super() constructor completes.

So...

When you construct the object... It calls the super constructor, which calls the showDevice() method (rule 2A). This calls the subclass showDevice() method (rule 1). Since, the instance variable has not been set yet -- that will be done during rule 2B, it has the default value of null. Hence, null is printed.

Henry
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry. That was tricky.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, nice question. I did not know that during overriding, if we have a method call in a constructor, then the overridden method will be called and also that Inheritance works, even when the object has not completed construction yet.

Thanks guys.
[ September 26, 2008: Message edited by: Arjun Reddy ]
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, then how is Mobile.showdevice() printed first, before null???
[ September 27, 2008: Message edited by: Abhi vijay ]
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay...
Now i am really getting confused..

Why is the showDevice() in Phone calling the method in Mobile..
I thought it was supposed to give the result "Phone device"

And I thought the instance variable are intiatlised before the constructor runs...

in the order

- Static variables /static block in Super class
- Static variables /static block in Sub class
-Instance variable/Init Blockes in Super class
- Super constructor
-Instance variable/Init Blockes in Sub class
- Sub constructor..
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhi vijay:
But, then how is Mobile.showdevice() printed first, before null???


Take a look at the code... The first one is the String. The second one is the instance variable being printed.



Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is the showDevice() in Phone calling the method in Mobile..
I thought it was supposed to give the result "Phone device"


Why do you think that inheritence should not work?


And I thought the instance variable are intiatlised before the constructor runs...


Instance variables are initialized before the constructor runs -- but it is not initalized before the super constructor runs. BTW, it would be a good idea to write a test program for this -- as that is the easiest way to remember the order.

Henry
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Henry said,
When you construct the object... It calls the super constructor, which calls the showDevice() method (rule 2A). This calls the subclass showDevice() method (rule 1). Since, the instance variable has not been set yet -- that will be done during rule 2B, it has the default value of null. Hence, null is printed.

If super constructor calls the showDevice() method, then why dosent the line
Phone.showDevice,null get printed.Why does it call the showDevice() method, in the sub class instead???
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow!! what a problem?

any way i am clear that when a call to overridden method is made in super constructor as part of construction of subclass object it will call the subclass method.mmmm... earlier i thought it would be super class's

kudos to vidhya and henry
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhi vijay:
As Henry said,
When you construct the object... It calls the super constructor, which calls the showDevice() method (rule 2A). This calls the subclass showDevice() method (rule 1). Since, the instance variable has not been set yet -- that will be done during rule 2B, it has the default value of null. Hence, null is printed.

If super constructor calls the showDevice() method, then why dosent the line
Phone.showDevice,null get printed.Why does it call the showDevice() method, in the sub class instead???


ahhh.... In the first paragraph, you acknowledge that inheritance will cause the subclass method to be called. In the second paragraph, you are questioning why is the subclass method being called.

Doesn't your first paragraph explain the question in the second paragraph? Or are you disagreeing with the first paragraph?

Henry
[ September 27, 2008: Message edited by: Henry Wong ]
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first paragraph was pasted from Henry's post...and the 2nd paragraph is my question.
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhi vijay:
The first paragraph was pasted from Henry's post...and the 2nd paragraph is my question.


Then back to my original question... Why do you think that inheritence should not work here?

Henry
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My doubt is Mobile() constructor will evoke Phone() constructor,right??? Then,instead of calling showDevice() in the Phone class, why is it calling showDevice() in the Mobile class?
 
Henry Wong
author
Marshal
Pie
Posts: 21518
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhi vijay:
My doubt is Mobile() constructor will evoke Phone() constructor,right??? Then,instead of calling showDevice() in the Phone class, why is it calling showDevice() in the Mobile class?


You are not answering my question. The showDevice() method has been overridden by the subclass. Why do you believe inheritence rules don't apply here?

But... to answer your question... It is calling the showDevice() method in the Mobile class, because it is a Mobile instance.

Henry
[ September 27, 2008: Message edited by: Henry Wong ]
 
Santhi Bharath
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhi

i think it is because we are constructing the sub class object.thats why it will call the sub class's method.

here in detail.

remember a point that

overridden methods are called using the object's visibility not the reference's visibility.

overloaded methods are called using the reference's visibility not the object's visibility.

here we are calling overridden method so it will call sub class's methods as the object is sub class's.


this is up to my knowledge...and waiting for corrections if any
 
vidhya suvarna
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Santhi, thats right what you have stated.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks santhi..i got the point.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic