Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Accessing instance variables of the super class

802930Oct 28 2010 — edited Nov 4 2010
Hi,

Please see the code below. If at *** variable "i" is not set to 4, the output will be 3, if it is set, it'll be 4.
My question is... well, I don't know exactly, there could be many. This whole thing is strange for me.
(For example it seems that the constructor for the super class runs when i access an instance variable from it.)
Could somebody explain this topic or maybe help me with directing to an article?
public class TryThis {
    
    public static void main (String args[]){

    	Child c = new Child();
    	c.seeSuper();   
    }
}

public class Parent {

	int i;
	
	Parent(){
		i = 3;
	}
}

public class Child extends Parent{

	Child(){
		//i = 4;  //***
	}
	
	public void seeSuper(){
		System.out.println(super.i);
	}
				
}
note: Parent could also simply look like this:
public class Parent {
	int i = 3;
}
This post has been answered by EJP on Nov 1 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 2 2010
Added on Oct 28 2010
24 comments
506 views