Post a Job  |   Ask a Question?   |   Already registered? login or register
saikumar gogulampati
27/01/2016 02:10 PM

J2SE(Core Java Question):Explain Constructor Order Dependencies?


Can anyone explain how your class calls the hierarchy of classes(parent-child) upto the Object class?
Spam    3 Comments

Basanta,Hota
27/01/2016 16:35:05
Due to default constructor see the flow class Test { Public Test (){ super (); } } We know every class is instance of subclass How. ..? Due to defult constructor. ..and super () paranthesis inside constructor through which it call the super class constructor. To make relationship

MD,Rahman
27/01/2016 16:53:30
As per my knowledge , constructor plays an important role in object creation too,in spite of initializing the instance variables.(However, this has contradiction also). While we call new operator with constructor (during creation of object) ,it first checks is my current class is extending any class or not ? If so,our class constructor first statement i.e. super() will call the super class constructor ,and so on. At last when a class is not extending any class then by default it extends the Object class (to get the property of java object and behaving like it). Hence first the object of Object class is created and hence known as root object , then the subclasses objects are formed.However, all the objects are created in association with each other. If you want furthur discussion ,you can.

saikumar,gogulampati
27/01/2016 22:19:00
yes, you people are right. As i feel that this question is very important for revision or for quick reference before interview and get rid of all complexity and drive confidence in answering the question, i posting this answer as it is from the book. Constructors in Extended Classes: --------------------------------------- An object of an extended class contains state variables (fields) that are inherited from the superclass as well as state variables defined locally within the class. To construct an object of the extended class, you must correctly initialize both sets of state variables. The extended class's constructor can deal with its own state but only the superclass knows how to correctly initialize its state such that its contract is honored. The extended class's constructors must delegate construction of the inherited state by either implicitly or explicitly invoking a superclass constructor. A constructor in the extended class can directly invoke one of the superclass's constructors using another kind of explicit constructor invocation: the superclass constructor invocation, which uses the super construct. If you do not invoke a superclass constructor or one of your own constructors as your constructor's first executable statement, the superclass's no-arg constructor is automatically invoked before any statements of the new constructor are executed. That is, your constructor is treated as if super(); were its first statement. If the superclass doesn't have a no-arg constructor, you must explicitly invoke another constructor. Constructors are not methods and are not inherited. If the superclass defines a number of constructors and an extended class wishes to have constructors of the same form, then the extended class must explicitly declare each constructor, even if all that constructor does is invoke the superclass constructor of the same form. Constructor Order Dependencies: ----------------------------------------------- When an object is created, memory is allocated for all its fields, including those inherited from superclasses, and those fields are set to default initial values for their respective types (zero for all numeric types, false for boolean, '\u0000' for char, and null for object references). After this, construction has three phases: 1. Invoke a superclass's constructor. 2. Initialize the fields using their initializers and any initialization blocks. 3. Execute the body of the constructor. First the implicit or explicit superclass constructor invocation is executed. If an explicit this constructor invocation is used then the chain of such invocations is followed until an implicit or explicit superclass constructor invocation is found. That superclass constructor is then invoked. The superclass constructor is executed in the same three phases; this process is applied recursively, terminating when the constructor for Object is reached because there is no superclass constructor at that point. Any expressions evaluated as part of an explicit constructor invocation are not permitted to refer to any of the members of the current object. In the second stage all the field initializers and initialization blocks are executed in the order in which they are declared. At this stage references to other members of the current object are permitted, provided they have already been declared. Finally, the actual statements of the constructor body are executed. If that constructor was invoked explicitly,then upon completion, control returns to the constructor that invoked it, and executes the rest of its body. This process repeats until the body of the constructor used in the new construct has been executed. If an exception is thrown during the construction process, the new expression terminates by throwing that exception no reference to the new object is returned. Because an explicit constructor invocation must be the first statement in a constructor body, it is impossible to catch an exception thrown by another constructor. (If you were allowed to catch such exceptions it would be possible to construct objects with invalid initial states.) Here is an example you can trace to illustrate the different stages of construction: class X { protected int xMask = 0x00ff; protected int fullMask; public X() { fullMask = xMask; } public int mask(int orig) { return (orig & fullMask); } } class Y extends X { protected int yMask = 0xff00; public Y() { fullMask |= yMask; } } If you create an object of type Y and follow the construction step by step, here are the values of the fields after each step: Step What Happens xMask yMask fullMask 0 Fields set to default values 0 0 0 1 Y constructor invoked 0 0 0 2 X constructor invoked (super) 0 0 0 3 Object constructor invoked 0 0 0 4 X field initialization 0x00ff 0 0 5 X constructor executed 0x00ff 0 0x00ff 6 Y field initialization 0x00ff 0xff00 0x00ff 7 Y constructor executed 0x00ff 0xff00 0xffff Understanding this ordering is important when you invoke methods during construction. When you invoke a method, you always get the implementation of that method for the actual type of the object. If the method uses fields of the actual type, they may not have been initialized yet. During step 5, if the constructor X invoked mask, it would use a fullMask value of 0x00ff, not 0xffff. This is true even though a later invocation of maskafter the object was completely constructedwould use 0xffff. Also, imagine that class Y overrides mask with an implementation that explicitly uses the yMask field in its calculations. If the constructor for X used the mask method, it would actually invoke Y's mask method, and at that point yMask would be 0 instead of the expected 0xff00. Methods you invoke during the construction phase of an object must be designed with these factors in mind. Your constructors should avoid invoking overridable methodsmethods that are not private, static, or final. If you do invoke such methods, clearly list them in your documentation to alert anyone wanting to override these methods of their potential unusual use.

Languages :
xml