Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Constructor doubt

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 99
Mac Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic