aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overridding query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overridding query " Watch "Overridding query " New topic
Author

Overridding query

vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
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


SCJP 1.4 - 88%<br />SCWCD 1.5 - Preparing
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
vidhya suvarna
Ranch Hand

Joined: Aug 28, 2008
Posts: 148
Thanks Henry. That was tricky.
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
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 ]

Be Humble... Be Nice.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
But, then how is Mobile.showdevice() printed first, before null???
[ September 27, 2008: Message edited by: Abhi vijay ]
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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..


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

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

Joined: Sep 16, 2008
Posts: 509
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

Joined: Jun 03, 2008
Posts: 75
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


thanks and regards<br />Santhi Bharath<br />SCJP 5.0, SCWCD 5.0
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

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

Joined: Sep 16, 2008
Posts: 509
The first paragraph was pasted from Henry's post...and the 2nd paragraph is my question.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

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

Joined: Sep 16, 2008
Posts: 509
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
Sheriff

Joined: Sep 28, 2004
Posts: 18715
    
  40

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

Joined: Jun 03, 2008
Posts: 75
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

Joined: Aug 28, 2008
Posts: 148
Yes Santhi, thats right what you have stated.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
thanks santhi..i got the point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overridding query