aspose file tools*
The moose likes Beginning Java and the fly likes Query about Subclass Constructor accessing instance variable of superclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Query about Subclass Constructor accessing instance variable of superclass " Watch "Query about Subclass Constructor accessing instance variable of superclass " New topic
Author

Query about Subclass Constructor accessing instance variable of superclass

Manish Dubey J
Greenhorn

Joined: Oct 08, 2011
Posts: 9

Why subclass constructor can't access instance variable of super class, (while it is executed before the subclass constructor)?


Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30293
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Manish Dubey J
Greenhorn

Joined: Oct 08, 2011
Posts: 9

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

Joined: Jul 27, 2010
Posts: 1157

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.


~ Mansukh
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Oct 08, 2011
Posts: 9

@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

Joined: Mar 17, 2011
Posts: 7635
    
  19

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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

Joined: Oct 08, 2011
Posts: 9

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

Joined: Mar 17, 2011
Posts: 7635
    
  19

Manish Dubey J wrote:It was one useful read.
Thank you!

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

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query about Subclass Constructor accessing instance variable of superclass