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%)<br />SCDJWS(94%)<br />next mission SCEA(but need to wait or that)
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...
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.
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(); }
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.
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.
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.