This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading,Overriding, Runtime Type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading,Overriding, Runtime Type" Watch "Overloading,Overriding, Runtime Type" New topic
Author

Overloading,Overriding, Runtime Type

kim jagota
Greenhorn

Joined: Aug 23, 2005
Posts: 9
Can someone please explain how the correct output is option 3?

Thanks.

(First lets put CODE tags around the code so we can read it. - Mark)


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
[ August 27, 2005: Message edited by: Mark Spritzler ]

(preparing for SCJP 1.4)
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

OK, so in main you create an instance of Mobile, which calls the Mobile no-args constructor, which calls super(), however in super it calls showDevice(), but which version. Since the instance is of type Mobile it calls the version on Mobile, but since the instance hasn't been created just yet, we are in that process right now the "device" instance variable has not been set. That is why the first call shows "Mobile.showDevice, null".

Basically the instance variable "device" of Mobile is not set till after the return from the super() call to the Mobile's superclass constructor.

Now the other two are printing "Mobile.showDevice, Mobile.showDevice" because it calls the showDevice() method on the instance type which is Mobile, and the "device" instance variable is now set to "Mobile.showDevice"

Does that help?

Mark
[ August 27, 2005: Message edited by: Mark Spritzler ]

Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
kim jagota
Greenhorn

Joined: Aug 23, 2005
Posts: 9
This helped. Thanks a lot!
Shrikant Pandhare
Greenhorn

Joined: Jul 29, 2005
Posts: 15
But again a bit confusion..
as per my understanding ... when instance is created
firstly instance variables are created , then they are initialised to default values, then instance initializer expressions and instance initializer blocks are executed and then constructor is called..
Then how come "device" is not initialised
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
In answer to the previous question: The instance variable Mobile.device has been initialized, to null. But it has not yet had the string "Mobil.device" assigned to it, because that is done at the stage when field initializers are performed.
[ August 28, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by Barry Gaunt:
In answer to the previous question: The instance variable Mobile.device has been initialized, to null. But it has not yet had the string "Mobil.device" assigned to it, because that is done at the stage when field initializers are performed.

[ August 28, 2005: Message edited by: Barry Gaunt ]


Yeah, that is what I meant to say instead of "instantiated", "assigned" is a better word to use.

Mark
satya mamillapalli
Ranch Hand

Joined: Jun 22, 2005
Posts: 63
class Phone {
String device = "Phone.device";
void showDevice() {
System.out.print("Phone.showDevice," + device + " ");
}
Phone() {
System.out.println("device," + device + " ");
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();
}
}



Why is the output says
device,Phone.device
Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.

How come the device instance variable got initiated in the superclass?

device,Phone.device
Dharmesh Gangani
Ranch Hand

Joined: Feb 20, 2004
Posts: 30
How come the device instance variable got initiated in the superclass?


This is because the device instance variable in the superclass in declared static.

- Dharmesh G.


-=-=-=-=-=-=-=-=-=-<br />Thanks & Regards,<br />Dharmesh G.
Jyothi Bhogadi
Ranch Hand

Joined: Jul 08, 2005
Posts: 47
hi dharmesh,
just take a look at the code given by satya.
The String in the super class is not static...
then how come is it giving the value of phone.device...

any idea.....

regards
Nilesh Raje
Ranch Hand

Joined: Aug 02, 2005
Posts: 153
Hello Friends,

Well further to this discussion i would Appreciate if any one could write down the exact flow of how the variable get instialised when an instance is created. ie example Mobile m =new Mobile()..! Now when this statement is executed what is the the actual flow of intialization ..!And one more point mentioned in the discussion that after the call returns from super only then the instance varaible get their actual value assigned..! Please help me to understand this flow so that I can apply this fundamental to all such questions..!

Thanks a ton in advance..!


Thanks and Regards,<br />Nilesh<br />SCJP 1.4, SCWCD 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading,Overriding, Runtime Type
 
Similar Threads
method overriding
Constructor
Overriding question
Class initialization order...
problem