Results 1 to 8 of 8
- 01-20-2010, 02:13 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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.
- 01-20-2010, 03:28 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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); } }
Jos
- 01-20-2010, 05:46 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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?
- 01-20-2010, 06:23 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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
- 01-20-2010, 09:22 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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.
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 ....
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?
- 01-20-2010, 09:31 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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?
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?
kind regards,
JosLast edited by JosAH; 01-20-2010 at 09:33 PM.
- 01-20-2010, 10:27 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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.
- 01-22-2010, 01:22 AM #8
Member
- Join Date
- Dec 2009
- Posts
- 76
- Rep Power
- 0
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
-
same object variable t and same methods is calling
By javastuden in forum New To JavaReplies: 1Last Post: 11-24-2009, 05:10 AM -
Calling methods from superclass
By moaxjlou in forum New To JavaReplies: 7Last Post: 12-11-2008, 01:07 AM -
Calling Methods
By bluegreen7hi in forum New To JavaReplies: 3Last Post: 12-17-2007, 07:22 AM -
Problems with readLine() and calling methods
By peachyco in forum New To JavaReplies: 2Last Post: 11-24-2007, 08:44 AM -
need help calling methods
By lowpro in forum New To JavaReplies: 2Last Post: 11-15-2007, 10:53 AM
Bookmarks