Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading,Overriding, Runtime Type

 
kim jagota
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • 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 ]
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
This helped. Thanks a lot!
 
Shrikant Pandhare
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • 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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • 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
 
satya mamillapalli
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • 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
 
Dharmesh Gangani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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.
 
Jyothi Bhogadi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • 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
 
Nilesh Raje
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • 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..!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic