Results 1 to 8 of 8
  1. #1
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default Calling subclassed methods from constructors

    From Object Initialization in Java
    Calling subclassed methods from constructors
    The strict ordering of instance variable initialization enforced by
    the Java compiler is, in part, an effort to ensure that during the initialization process,
    instance variables are never used before they have been initialized to their proper initial values.
    As illustrated earlier in this article, however, the rules of ordering are not bulletproof.
    There are ways you can use an instance variable during initialization before it has been initialized to its proper value,
    while it still has its default value. In the case of instance variable initializers, you can invoke a method
    that uses a variable declared textually after the variable being initialized.
    Another way to use an instance variable before it has been properly initialized is to invoke a method from a superclass initializer or constructor that uses instance variables in a subclass.

    Hello
    I did a part of above text whit bold face.
    I do not undrestand this part at all.
    Please example for me.
    Thank you.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by arefeh View Post
    Hello
    I did a part of above text whit bold face.
    I do not undrestand this part at all.
    Please example for me.
    Thank you.
    Run this little monster and try to understand it; it (ab)uses a variable from a subclass and the output shows when exactly the initialization takes place:

    Java Code:
    abstract class BaseClass {
    	
    	abstract protected void initialize(String name);
    	
    	public BaseClass(String memberName) {
    		
    		initialize(memberName);
    		System.out.println("in base: "+this);		
    	}
    }
    
    public class DerivedClass extends BaseClass {
    	
    	String name= "bar";
    	
    	public DerivedClass(String memberName) {
    		super(memberName);
    	}
    	
    	protected void initialize(String name) { this.name= name; }
    	
    	public String toString() { return name; }
    	
    	public static void main(String[] args) {
    		
    		DerivedClass dc= new DerivedClass("foo");
    		
    		System.out.println("DerivedClass: "+dc);
    	}
    }
    kind regards,

    Jos

  3. #3
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    I want to describe what i understood from this prrogram.
    1)at first: name is null
    2)Next: calling DerivedClass's constructor and geting "foo" and calling super("foo").
    3)Next: calling BaseClass's constructor and geting "foo" and calling initialize("foo").
    4)Next: We go down to DerivedClass and call protected void initialize("foo"), then this.name="foo".
    5)Next: We go up to BaseClass, then System.out.println("in base: "+this).
    6)Next: now DerivedClass's constructor is run and name field gets "bar".
    7)Next: System.out.println("DerivedClass: "+dc);.

    Question 1: all parts(1-7) are true?
    Question 2: I could not understand that how initialize(memberName); in BaseClass's constructor used from
    protected void initialize(String name) in DerivedClass class?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by arefeh View Post
    Hello
    I want to describe what i understood from this prrogram.
    1)at first: name is null
    2)Next: calling DerivedClass's constructor and geting "foo" and calling super("foo").
    3)Next: calling BaseClass's constructor and geting "foo" and calling initialize("foo").
    4)Next: We go down to DerivedClass and call protected void initialize("foo"), then this.name="foo".
    5)Next: We go up to BaseClass, then System.out.println("in base: "+this).
    6)Next: now DerivedClass's constructor is run and name field gets "bar".
    7)Next: System.out.println("DerivedClass: "+dc);.

    Question 1: all parts(1-7) are true?
    Question 2: I could not understand that how initialize(memberName); in BaseClass's constructor used from
    protected void initialize(String name) in DerivedClass class?
    Step 6) is in more detail: after the superclass construct has run but before the DerivedClass constructor is run the explicit initializers are run and name is initialized to the value "bar" so value "foo" is gone.

    About question 2) the base class constructor is run as part of the initialization of a DerivedClass object. The superclass is abstract but the overriding mechanism is already installed so an initialize() method from the DerivedClass is called as part of the execution of the superclass constructor code.

    kind regards,

    Jos

  5. #5
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    Thank you about your nice answers.
    Now, i have 3 questions about this subject.
    one:
    the base class constructor is run as part of the initialization of a DerivedClass object.
    we know that constructor's methods go inheritance down to up, but initialization of fields goes inheritance down to up.
    i.e the superclass is not aware about its subclass. ok?
    but you said:"the base class constructor is run as part of the initialization...."
    I feel that you said this text about current program, not about all.ok?

    two:
    The superclass is abstract but the overriding mechanism is already installed ....
    i.e this subject is a rule?

    three:
    when we write DerivedClass dc= new DerivedClass("foo");
    i.e we are pointing to the top of DerivedClass class?
    i.e we are pointing to the place of name field in memory?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by arefeh View Post
    Hello
    Thank you about your nice answers.
    Now, i have 3 questions about this subject.
    one:

    we know that constructor's methods go inheritance down to up, but initialization of fields goes inheritance down to up.
    i.e the superclass is not aware about its subclass. ok?
    but you said:"the base class constructor is run as part of the initialization...."
    I feel that you said this text about current program, not about all.ok?
    All constructors do that: befefore a derived class's constructor is run first its superclass's constructor is run and then the initialization of the member variables is run (from top to bottom through the object) and only then is the body for the derived class executed; just like an onion: inner layers are constructed first.

    two:

    i.e this subject is a rule?
    Yep, see above.

    three:
    when we write DerivedClass dc= new DerivedClass("foo");
    i.e we are pointing to the top of DerivedClass class?
    i.e we are pointing to the place of name field in memory?
    You are thinking much too C/C++-ish: Java has references and they refer to an object. The bare metal fact that these references are pointers doesn't count.

    kind regards,

    Jos
    Last edited by JosAH; 01-20-2010 at 08:33 PM.

  7. #7
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Hello
    Again I want to describe what i understood from this program:
    1)At first: calling DerivedClass dc= new DerivedClass("foo");.
    2)Next: calling public DerivedClass(String memberName).
    3)Next: calling public BaseClass(String memberName).
    4)Next: runnig System.out.println("in base: "+this); and return.
    5)Next: runnig String name= "bar";.
    6)Next: return.
    7)Next: producing a reference with the name ofthis that is secret.
    8)The end: running System.out.println("DerivedClass: "+dc);.

    Now:
    -dc in System.out.println("DerivedClass: "+dc); is that reference with the name of[this]?
    -Now, to where dc is pointing?
    -this is a reference for DerivedClass and BaseClass that becomes one object in memory?
    -I yet did not understand this:the base class constructor is run as part of the initialization of a DerivedClass object.


    I am very sorry that i have many questions about this subject.

  8. #8
    arefeh is offline Member
    Join Date
    Dec 2009
    Posts
    76
    Rep Power
    0

    Default

    Java Code:
    class A
    {
    	private int a=7;
    	private int b=8;
    	A()
    	{
    	}
    	A(int a)
    	{
    		super();
    		System.out.println("*a= "+a);//a=2
    		System.out.println("*b= "+b);//b=8
    		this.a=a;
    		System.out.println("a= "+a);//a=2
    	}
    	A(int a, int b)
    	{
    		this(a);
    		System.out.println("*a= "+a);//a=2
    		System.out.println("*b= "+b);//b=5
    		this.b=b;
    		System.out.println("b= "+b);//b=5
    	}
    	int addab()
    	{
    		return a+b;
    	}
    }
    
    class B extends A
    {
    	B(int a, int b)
    	{
    		super(a,b);
    	}
    }
    
    public class Row_OF_This
    {
    	public static void main(String args[])
    	{
    		B ob=new B(2,5);
    		System.out.println("a+b= "+ob.addab());//a+b=7
    		System.out.println("witch field? "+ob);//?
    	}
    }

    Hello

    I run above program and got results insiso of commands.
    Please pay attention to this program.
    I want to describe what i understood from this program:

    The first step is a place for variables a,b and native pointer to class information on HEAP. Then getting default value for variables.

    1)B ob=new B(2,5);
    2)A(int a, int b)
    3)this(a);
    4)super();
    5)a=7 and b=8
    6)System.out.println("*a= "+a);//a=2 and System.out.println("*b= "+b);//b=8
    this.a=a; and System.out.println("a= "+a);//a=2
    7)System.out.println("*a= "+a);//a=2 and System.out.println("*b= "+b);//b=5
    this.b=b; and System.out.println("b= "+b);//b=5
    8)return to class B
    9)return to main and producing this reference
    10)System.out.println("a+b= "+ob.addab());//a+b=7
    11)System.out.println("witch field? "+ob);//?

    OK?

    we know that after producing reference:
    System.out.println("a+b= "+ob.addab()); is equal System.out.println("a+b= "+ob.addab(B this)); for class file

    OK?

    we know that after producing reference:
    this.a or this.b

    OK?

    then in System.out.println("witch field? "+ob); ob is this.OK? ob shown an unmeaning things
    then dc in System.out.println("DerivedClass: "+dc); is this.OK? dc shown "bar"

    why?

Similar Threads

  1. same object variable t and same methods is calling
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 11-24-2009, 04:10 AM
  2. Calling methods from superclass
    By moaxjlou in forum New To Java
    Replies: 7
    Last Post: 12-11-2008, 12:07 AM
  3. Calling Methods
    By bluegreen7hi in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:22 AM
  4. Problems with readLine() and calling methods
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-24-2007, 07:44 AM
  5. need help calling methods
    By lowpro in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •