• 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

problem

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shree shree,

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

Nick
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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 ]
 
Raghu Shree
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Could u explain the output of the previous program.
 
sethu chiyan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic