aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor doubt 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 "Constructor doubt " Watch "Constructor doubt " New topic
Author

Constructor doubt

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Hi,
In following code the o/p is Mobile.showDevice,null
Mobile.showDevice,Mobile.device.
As Class Mobile is extending Phone class when we are instantiating the Mobile class it should call Phone class constructor . why there is no call for super().
Can any one explain me where I am wrong.

Thanks in advance


SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Pooja Jindal
Greenhorn

Joined: Aug 08, 2007
Posts: 10
we have not explicitly put a call to super() but
compiler would automatically put a call to super().
So the o/p would be

Phone.showDevice Phone.device
Mobile.showDevice Mobile.device
Mobile.showDevice Mobile.device
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by Pooja Jindal:
we have not explicitly put a call to super() but
compiler would automatically put a call to super().
This is correct.

So the o/p would be
Phone.showDevice Phone.device
Mobile.showDevice Mobile.device
Mobile.showDevice Mobile.device
But that not.

Tip: It's crucial which showDevice() method is called.
Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99

Tip: It's crucial which showDevice() method is called.


Yeah, what Manfred said is true. It is really crucial when a method is overridden in the subclass and that method called from the super(). super() will always call that class's method whose object you are instantiating.

In this case Mobile class constructor will call the super class constructor and super will call the method showDevice() of the subclass. But since the subclasses variables are not initialized yet, so device = null
Hence the output will be...

Mobile.showDevice,null
Mobile.showDevice,Mobile.device
Mobile.showDevice,Mobile.device


MooN
Himanshu Saxena
Greenhorn

Joined: Jul 18, 2007
Posts: 5
Hi
First of all I want to tell that the default constuctor is called.
Second the Variables depend on the refernce type while the methods depend on the object type.

Try this:

class Phone {
static String device = "Phone.device";
void showDevice() {
System.out.println("Phone.showDevice," + device + " ");
}
Phone() {
System.out.println("Device:"+device);
showDevice();
}
}

class Mobile extends Phone {
String device = "Mobile.device";
void showDevice() {
System.out.println("Mobile.showDevice," + device + " ");
}
Mobile() {
System.out.println("Device:"+device);
showDevice();
}
public static void main(String[] args) {
Mobile n = new Mobile();
n.showDevice();
}
}

Output:
Device hone.device
Mobile.showDevice,null
Device:Mobile.device
Mobile.showDevice,Mobile.device
Mobile.showDevice,Mobile.device
suneel kumar
Ranch Hand

Joined: Jan 08, 2007
Posts: 46
In the above program , the method showDevice is overrieding..

/*
* ConstructorDemo.java
*
* Created on August 9, 2007, 2:39 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package com;

/**
*
* @author suneelkumarr
*/
class Phone{
Phone(){
System.out.println("inside Phone constructor");
display();
}
void display(){
System.out.println("inside display method Phone class");
}
}
public class ConstructorDemo extends Phone{

/** Creates a new instance of ConstructorDemo */
public ConstructorDemo() {
System.out.println(" the ConstructorDeminsideo");
display();
super.display();
}
void display(){
System.out.println("inside the display in Constructor Demo ");
}
public static void main(String args[]){
ConstructorDemo constructorDemo = new ConstructorDemo();
constructorDemo.display();
}

}


try this , it will clear your doubts.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Yes I agree with the explanation above. Here is what I have understood:

A class constructor can be called due to 2 reasons:
-Same class object is being instantiated
-Sub class object is being instantiated
Inside the constructor, the methods are bound to the object which is causing the constructor to be invoked. Hence if it is the same class, then the method of the same class is invoked while if it is a subclass, the overridden version of the method in the subclass is invoked. Now any member variable references made inside this overridden method called inside the constructor, are resolved as per the class object on which this method is being invoked, i.e. if its the same class, then the same class member variable is used and if its a subclass, it results in null(or default value in case of primitives) as the subclass instance members have not yet been initialized.

Please correct me if you find something wrong here.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Satya Maheshwari:

Please correct me if you find something wrong here.


Thats correct.

If you have an IDE which supports the debugging facility, just run this program in debug mode, which will help you in understanding the object initialization sequence very well.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
I would go with Al Mamun! and to add two things further,

First "this" always refers to the current-object, and
second, "always the superclass is initialized first before its subclass."


Originally posted by Himanshu Saxena:

...the Variables depend on the refernce type while the methods depend on the object type.



To a great extent this code too will seem to be proving your point,


But the real reason I consider is

A reference to a class field causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface.


There are many good things at this place ... you make like to explore, especially those examples out there.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor doubt