Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Constrcutors!  RSS feed

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code gives the output as
Mobile.showDevice,null Mobile.showDevice,Mobile.device Mobile.showDevice,Mobile.device Can anybody pl explain.Thanks


 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here concept of constructor chaining comes in to picture. In main() we are constructing an object of Mobile class. "Mobile n=new Mobile();"

This will invoke the default constructor of Mobile class. Due to constructor chaining super class(Phone) default constructor will be called. It is the object which decides which method to be called. Here object is of Mobile class so showDevice() of Mobile will be called. It will print "Mobile.showDevice, null".
Then Mobile() will be called and it's showDevice() will be called which will print "Mobile.showDevice,Mobile.device". We created an object n of Mobile class which invokes method showDevice() so it will be executed and print "Mobile.showDevice,Mobile.device".

If in case you declare String device as static :

public class Mobile extends Phone {
static 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();
}
}

The output will be as follows
Mobile.showDevice,Mobile.device Mobile.showDevice, Mobile.device Mobile.showDevice, Mobile.device
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have put some efforts to elaborate why the output came as you descibed.
If any constructor gets called the followings steps are followed:

STEP A * Set fields to default initial values (0, false, null)
STEP B * Call the constructor for the object (but don't execute the body of the constructor yet)
STEP C * Invoke the superclass's constructor
STEP D * Initialize fields using initializers and initialization blocks
STEP E * Execute the body of the constructor



Basically ,The instance variables are initialized before the execution of the body of constructor


- The analysis for two lines in main are listed as follows:

Mobile n = new Mobile();

calls Mobile() constructor

Set fields of instance variables of class "Mobile" to defaults (0, false, null);
So Mobile classes String device instance variables gets the value as : device = null

Call the constructor for the object

The first line implicitly calls to super()

Phone() constructor gets called,

-Set fields of instance variables of class Phone to defaults (0, false, null)
-calls the constructor for the object
-initilizes Phone classe's instance variables:=> String device = "Phone.device";
-Calls ALREWADY OVERIDDEN version of showDevice()
-It SUPPOSE To PRINT "Mobile.showDevice,"+Mobile instance variable "device" => "Mobile.showDevice,null"


The second line calls showDevice() which calls OVERIDDEN version of showDevice() and prints
"Mobile.showDevice,Mobile.device"


n.showDevice();
Here the instance of Mobile : n calls the method showDevice() and prints
"Mobile.showDevice,Mobile.device"


One may verify the concepts described above by changes at @(A), @(B) @(C):
The output is listed at the end of the code.



Output

device phone->Phone.device [Changes in @(A)]
Mobile.showDevice,null
device Mobile phone Mobile.device[Changes in @(B)]
device phone->Phone.device[Changes in @(C)]
Mobile.showDevice,Mobile.device
Mobile.showDevice,Mobile.device

Please feel free to reiterate any of the points missed in this regards.

Thanks
Prithwish
 
author
Sheriff
Posts: 8977
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors! Are so exciting!! Let's all SHOUT!!! about them !!!
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well explained Prithwish. You even cleared my concepts for constructor chaining.

Sandeep
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!