Forums Register Login

About Constructor Chaining

+Pie Number of slices to send: Send


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?
+Pie Number of slices to send: Send



Now try to get , why the output is like this ....
[ April 02, 2005: Message edited by: rathi ji ]
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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".
+Pie Number of slices to send: Send
anyone?
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 


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 ]
    +Pie Number of slices to send: Send
    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
    +Pie Number of slices to send: Send

    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 ]
    +Pie Number of slices to send: Send
    hi tony
    i fear to say that i didn't get that point can u please explain me with some detailed code.
    +Pie Number of slices to send: Send
    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
    Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 798 times.
    Similar Threads
    super . super
    Dynamic polymorphism and Overriding
    Decison between overriding methods & arithmetic prmotable overloaded methods
    clarification in code
    SCJP questions
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Mar 29, 2024 01:19:30.