• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloading,Overriding, Runtime Type

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
kim jagota
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This helped. Thanks a lot!
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..!
 
I am displeased. You are no longer allowed to read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic