Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes problem 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 "problem" Watch "problem" New topic
Author

problem

Soni Prasad
Ranch Hand

Joined: Mar 09, 2005
Posts: 97
1. class Phone {
2. static String device = "Phone.device";
3. void showDevice() {
4. System.out.print("Phone.showDevice," + device + " ");
5. }
6. Phone() {
7. showDevice();
8. }
9. }

1. class Mobile extends Phone {
2. String device = "Mobile.device";
3. void showDevice() {
4. System.out.print("Mobile.showDevice," + device + " ");
5. }
6. Mobile() {
7. showDevice();
8. }
9. public static void main(String[] args) {
10. Mobile n = new Mobile();
11. n.showDevice();
12. }
13. }

result of the above code is :

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

I coulod not understand-
The Phone costructor should call its own showDevice() method but it is calling showDevice method of Mobile class???


SCJP 1.4, SCBCD 1.3
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi
Sorry for repating your code here


In subclass constructor you don't called the super class constructor. So compiler automatically add the super() method in subclass constructor.
In your examples subclass constructor calls the superclasss construtor. Here showDevice() method is overriding and the result is printed. But my doubt in your code is why "Mobile.showDevice, null"

I need your suggestions


Raghu J<br />SCJP 1.4<br /> <br />The Wind and waters are always<br />on the side of the ablest navigators.<br /><a href="http://groups.yahoo.com/group/scjp_share" target="_blank" rel="nofollow">SCJP Group</a><br /><a href="http://groups.yahoo.com/group/JavaBeat_SCWCD" target="_blank" rel="nofollow">SCWCD Group</a>
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
shree shree,

Your name violates the Naming Policy, please change it at Your Profile.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
"null" is getting printed because the call to the constructor is not completed hence the member variables are not initilized.
[ April 06, 2005: Message edited by: Srinivasa Raghavan ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi
Yes. when superclass constructor is completed then subclass memebers are initialized. Then method calling is based on Object. Now I have changed this code like below



Both of the classes have device field. I know member variable accessed based on reference varaible. So I think the output is like below

Phone.showDevice, Mobile.device Phone.showDevice, Mobile.device Phone.showDevice, Mobile.device

But the output is
Phone.showDevice, Phone.device Phone.showDevice, Phone.device Phone.showDevice, Phone.device

I am confusted. AnyThing I missunderstand here
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
The overriden method is called with respect to the object.
The child class is the one called the super class constructor, so this overriden method is called with reference to the child class object and hence the method in child gets executed.
sethu chiyan
Greenhorn

Joined: Apr 05, 2005
Posts: 10
Hi Prasad n Raghu,
Note the ERROR:
class Mobile extends Phone
{
String device = "Mobile.device";
void showDevice()
{
super();//Compiler objects
//Coz the Constructor call
//should be the first statement of the constructor
System.out.print("Mobile.showDevice," + device + " ");
}
Mobile()
{
super();// Complier's automatic constructor calls from here
showDevice();
}

public static void main(String[] args)
{
Mobile n = new Mobile();
n.showDevice();
}

}
With regards,
Chiyan
Raghu Shree
Ranch Hand

Joined: Mar 18, 2005
Posts: 143
Hi,
Could u explain the output of the previous program.
sethu chiyan
Greenhorn

Joined: Apr 05, 2005
Posts: 10
Hi,
Apologize for repeating the whole prg.
class Phone
{
String device = "Phone.device";
void showDevice()
{
System.out.print("Phone.showDevice," + device + " ");
}
Phone()
{
showDevice();
}
}
class Mobile extends Phone
{
String device = "Mobile.device";
void showDevice()
{
/*super();Compiler objects: Coz the Constructor call should be the first statement of the constructor*/
System.out.print("Mobile.showDevice," + device + " ");
}
Mobile()
{
super();// Complier's automatic constructor calls from here
showDevice();
}
public static void main(String[] args)
{
Mobile n = new Mobile();
n.showDevice();
}
}

The O/P of the prg is

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

Explanation:
while creating the instance for class Mobile(), its constructor is called. From the Mobile Constructor it invokes parent class's Phone constructor either u define super(); explicitly or implicitly.

As Srinivas said:

"null" is getting printed because the call to the constructor is not completed hence the member variables are not initilized.

The rest of the O/P is same as explained above.

If u want to call a overriden method from overriding method ,u can use

void showDevice()
{
super.showDevice();// way of using super inside the method
}

with regards,
Chiyan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem
 
Similar Threads
method overriding
Constructor
Overriding question
Overloading,Overriding, Runtime Type
Class initialization order...