Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query about Subclass Constructor accessing instance variable of superclass

 
Manish Dubey J
Greenhorn
Posts: 9
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why subclass constructor can't access instance variable of super class, (while it is executed before the subclass constructor)?


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34378
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great question. Consider the following example



It outputs:

method called before super
color created
Constructor of superClass red
Constructor of subClass Green


In particular, note that the getColor() method is called before the superclass is instantiated. This corresponds to super(green). At the point in time green is evaluated, the superclass green variable doesn't yet exist.
 
Manish Dubey J
Greenhorn
Posts: 9
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does that mean- instance initializer block of superclass("color created") runs 'just' after the statement 'super(getColor)'; completes?
Also please specify whether it is special case of explicit constructor invocation?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Jeanne, my understanding of the way constructors have been designed to run in Java the way they do(i.e. all super class constructors will run and the sub class constructor will run last) is exactly because of this reason. The sub class might try and access/modify an instance variable of the super class while the instance itself does not exist yet. Hence, this is the compiler's way of ensuring that the developers instantiate the super class before trying to access its fields in sub class.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish Dubey J wrote:Does that mean- instance initializer block of superclass("color created") runs 'just' after the statement 'super(getColor)'; completes?
Also please specify whether it is special case of explicit constructor invocation?


If you make color a static member, then it gets loaded at compile time along with the class(es). Hence, it works. But in the former case, because there is no CardObject(super class) instance, there is no color = "green"(instance variable copy on the heap). So the compiler complains.
 
Manish Dubey J
Greenhorn
Posts: 9
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Mansukh Yes I knew the static part, but please comment on this- "Instance initializer block of superclass("color created") runs 'just' after the statement 'super(getColor)'; completes??" because the subclass constructor's body gets executed after Instance Initailizer block of superclass but "super();" statement executes before it instance Initialization block.

Mansukhdeep Thind wrote:Hence, this is the compiler's way of ensuring that the developers instantiate the super class before trying to access its fields in sub class.

I think that is the whole gist.

 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish Dubey J wrote:because the subclass constructor's body gets executed after Instance Initailizer block of superclass but "super();" statement executes before it instance Initialization block.

I think you're overthinking this by a country mile.

Consider the statement:
super(color);

Assuming for a moment that the call actually had a color value that it could use, why would you be making such a call?

When you answer that question, you will understand why you're getting exactly the error you are.

PS: You plainly haven't read my StringsAreBad page yet.

Winston
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The very purpose of having instance initializer blocks(as their name suggests) is to assign values to instance variables. The rule is that the compiler will copy the instance initializer blocks into all the constructors of a class. Now can you make sense of what is happening? Whenever an instance of CardObject is created, the compiler copies the line into its constructor as its first line. Let me know in case of any other queries.
 
Manish Dubey J
Greenhorn
Posts: 9
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: why would you be making such a call?

That gave some food for introspection. I think that is unnecessary.

Winston Gutkowski wrote:
PS: You plainly haven't read my StringsAreBad page yet.

It was one useful read.
Thank you!
Mansukhdeep Thind wrote:
The rule is that the compiler will copy the instance initializer blocks into all the constructors of a class.

If thats the rule, then I should have known it earlier. Thanks for coming to my Rescue. All of you.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish Dubey J wrote:It was one useful read.
Thank you!

You're most welcome. Glad someone "got it".

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic