aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About Constructor Chaining 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 "About Constructor Chaining" Watch "About Constructor Chaining" New topic
Author

About Constructor Chaining

Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200


This code prints
You are inside method 1 of Revolution
0
30

I'm a little bit confused regarding the topic of constructor chaining, can someone provide me some tips in here?


Be Afraid...Be very Afraid...
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830



Now try to get , why the output is like this ....
[ April 02, 2005: Message edited by: rathi ji ]
MI Mohammed
Ranch Hand

Joined: Feb 16, 2005
Posts: 146
sorry i have to recopy the code.
ok am going to use comments to illustrate.
class Matrix {
int i; // matrix i
String string;// matrix String
public Matrix(){
this.method1(); // call matrix method1
System.out.println(i);// matrix 1 = 0;
}
public void method1(){
System.out.println("You are inside method 1 of Matrix");
}
}
class Reloaded extends Matrix{
int i; // Reload i (this shadows inherited matrix i)
String string; // shadows inheritd matrix string
public void method1(){
System.out.println("You are inside method 1 of Reloaded");
} // Override matrix method1
}
class Revolution extends Reloaded{
int i =30; // shadows Reload inherited i
Revolution(){
System.out.println(i); // i = 30
}
public void method1(){
System.out.println("You are inside method 1 of Revolution");} // overrides Reloaded method1
} public class Chaining{
public static void main(String[] args){
Revolution revolution = new Revolution(); // call Revolution constructor
}
}
Lets trace the chain now.
Revolution revolution = new Revolution()
call Revolution's constructor.
the constructor puts a call to super() to
Reloaded before the print statement.
Reloaded also put a super() call to Matrix constructor.
Inside the Matrix constructor,method1 of Matrix is called
it prints "You are inside method 1 of Matrix" then prints the value of
Matrix i = 0(default value of int).
the next bustop is the Reloaded default constructor but nothing happens there.
Then the final bustop, the Revolution constructor,
which set i = 30. Hu la la.
Key Points to note.
1. When subclass inherit a variable from it super class
and redefined it, the subclass version over shadows the
inherited version.(Remember in java you can't override veriables.
it is intialised at compile time. ush.

2. Method overriding is resolved at runtime.
so, the class is used to resolve the version to call.
While for variable,the object reference is used.

Whao, hope it helps.
It's better to bleed during peace time, so
that you sweetless during war(java cert 4 U).


SCJA(Beta) SCJP 1.4 SCWCD 1.4 SCBCD 1.3 SCBCD 5.0 beta <br />The more practice we get, the better we are at the exams and in life in general. Pls join me at My DEN.
Anwar Hossain
Ranch Hand

Joined: May 29, 2002
Posts: 35
Dear Mohammad,
If you want to override any method from the super class to subclass then it'll look first at the constructor of the super class then it'll execute first,otherwise it'll through a compile time error.

So try to learn it from Khalid-a-Mughals SCJP certification book.okay.

Regards
Anwar Hossain
SCJP2,MCP
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
Originally posted by mi Mohammed:
sorry i have to recopy the code.
ok am going to use comments to illustrate.
class Matrix {
int i; // matrix i
String string;// matrix String
public Matrix(){
this.method1(); // call matrix method1
System.out.println(i);// matrix 1 = 0;
}
public void method1(){
System.out.println("You are inside method 1 of Matrix");
}
}
class Reloaded extends Matrix{
int i; // Reload i (this shadows inherited matrix i)
String string; // shadows inheritd matrix string
public void method1(){
System.out.println("You are inside method 1 of Reloaded");
} // Override matrix method1
}
class Revolution extends Reloaded{
int i =30; // shadows Reload inherited i
Revolution(){
System.out.println(i); // i = 30
}
public void method1(){
System.out.println("You are inside method 1 of Revolution");} // overrides Reloaded method1
} public class Chaining{
public static void main(String[] args){
Revolution revolution = new Revolution(); // call Revolution constructor
}
}
Lets trace the chain now.
Revolution revolution = new Revolution()
call Revolution's constructor.
the constructor puts a call to super() to
Reloaded before the print statement.
Reloaded also put a super() call to Matrix constructor.
Inside the Matrix constructor,method1 of Matrix is called
it prints "You are inside method 1 of Matrix" then prints the value of
Matrix i = 0(default value of int).
the next bustop is the Reloaded default constructor but nothing happens there.
Then the final bustop, the Revolution constructor,
which set i = 30. Hu la la.
Key Points to note.
1. When subclass inherit a variable from it super class
and redefined it, the subclass version over shadows the
inherited version.(Remember in java you can't override veriables.
it is intialised at compile time. ush.

2. Method overriding is resolved at runtime.
so, the class is used to resolve the version to call.
While for variable,the object reference is used.

Whao, hope it helps.
It's better to bleed during peace time, so
that you sweetless during war(java cert 4 U).



Based on your post above, you mentioned that
Inside the Matrix constructor,method1 of Matrix is called
it prints "You are inside method 1 of Matrix"
gets printed out , but when I run the code, it prints out "You are inside method 1 of Revolution". That's why I'm confused, if I would be asked what would be the answer if that code is run, I would say "You are inside method 1 of Matrix".
Paulo Aquino
Ranch Hand

Joined: Apr 29, 2002
Posts: 200
anyone?
Yingtao Jiang
Greenhorn

Joined: Nov 05, 2002
Posts: 13
Hi, Paulo,

Inside the Matrix constructor,method1 of Matrix is called
it prints "You are inside method 1 of Matrix"


This statement is wrong... unless the "method1()" in Matrix have an access modifier "private".

The method inside Matrix called is "this.method1()". If the Matrix class defined a private method1(), then, the method call will be binded at compile time. Otherwise(not a private--protected, default,public-- means possibly be overriden in subclass), the method call is dynamically binded. It will call the method1 of "this" object--i.e. the receiver object (in this case, the Revolution object). If the runtime can not find a method1() in the receiver object, then it will climb up the inherientance tree untill it find one or declare an compile error.

Also, IMO, it NEARLY always a design error/flaw to call non-private method inside a constructor. As the called method could be override, and the behavior is undefined (out of your control). How could you use that kind of method to "initialize" the state of your object? The only exception of this kind of use mightbe in designing a hooking class in a framework intented to be override, and the document clearly specified the intended behavior of the method to be overriden by the subclass.

Hope my explaination helps.


SCJP1.4<br />SCJD1.4 (preparing)
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

it NEARLY always a design error/flaw to call non-private method inside a constructor.


Correction.
It is always a severe design flaw when you call an overrideable method from a constructor.
An overrideable method is defined as one that is all of the following:
  • not declared final
  • not declared static
  • not declared private
  • declared in a class that is not declared final
  • declared in a class that exposes at least one constructor that is not private, unless the class is a nested class, in which case, this rule does not apply

  • [ April 04, 2005: Message edited by: Tony Morris ]

    Tony Morris
    Java Q&A (FAQ, Trivia)
    Parameswaran Thangavel
    Ranch Hand

    Joined: Mar 01, 2005
    Posts: 485
    hi tony
    i didn't get this point can u explain me what it means

    declared in a class that exposes at least one constructor that is not private, unless the class is a nested class, in which case, this rule does not apply
    Tony Morris
    Ranch Hand

    Joined: Sep 24, 2003
    Posts: 1608

    The method m is not overrideable because a subclass of X is not possible. On another note, this class should be declared final as a matter of good form.

    The method m in this case is overrideable because it is a member of a nested class - it is called from a constructor. This is defective code.
    [ April 04, 2005: Message edited by: Tony Morris ]
    Parameswaran Thangavel
    Ranch Hand

    Joined: Mar 01, 2005
    Posts: 485
    hi tony
    i fear to say that i didn't get that point can u please explain me with some detailed code.
    MI Mohammed
    Ranch Hand

    Joined: Feb 16, 2005
    Posts: 146
    Aquino,
    Thanks for your observation, i had to work it out
    again. i will just explain what happened.
    let stsrt form
    Revolution r = new Revolution();

    In overriding method, the class is used and selected at runtime. ok
    For the variable the reference is used and selected at compile time.

    when method1 called from Matrix constructor, Revolution() was used, so
    so Revolution method1 was called.The this keyword does not have any effect
    on the method call. Remove method1 from Revolution, the what get call is the Matrix mehod1.

    Hope it's help.
    cheers
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: About Constructor Chaining