aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Source: Devaka Cooray's Simulator( made some modifications)



The answer is Demo3 Demo3.
But how is super.s returning the same value.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

super.s and s both refer to the same field s of XClass. So the output is normal. What are you confused at?? If you thought that super.s would display 2, then think about it. writing super.s will not call runDemo method in XClass. So the output will contain 3 i.e. the value assigned to s in the initializer of YClass...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
As subclass and superclass is sharing same instance variable, as we did not hide instance variable in subclass...
{
s=3;
}


This statement in subclass will set the value of 3 that will accessed by super.s or s.


SCJP 6
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
The flow of the program.
XClass is loaded, value of s is set as 1.

The instance initializer in XClass does not run, as it is not instantiated.//Is this right???

Then instance initializer of YClass euns, sets the value of s=3.
Since s is not declared in YClass, it is the same as that of super class.
So the answer is 3.


Am I right???
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
NO...........

The instance initializer in XClass does not run, as it is not instantiated.//Is this right???


Remember: when ever you instantiate subclass, it first calles all superclass' constructors, so instance initializer in superclass will run.

Just comment instance initializer in subclass and run your program again.
see what happens...
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

You are half right.

XClass is loaded, value of s is set as 1.


s is an instance field. So it will be set when instance of XClass or it's subclass is created.

he instance initializer in XClass does not run, as it is not instantiated.


Again wrong. The initialization expression i.e. when s is set to 1 will be executed when an instance of XClass or it's subclass is created. Then the initializer block will execute and it will set the value of s to 2;

Then instance initializer of YClass euns, sets the value of s=3.
Since s is not declared in YClass, it is the same as that of super class.


Right this time. There is no s in YClass so s and super.s refer to the same field.

So the flow of execution goes like this

XInterface xi=new YClass();

is executed. As a consequence first int s=1; is executed so s is set to 1. Then

{
s=2; //in class XClass
}

is executed so s is set to 2. Then

{
s=3; //in class YClass
}

is executed so s is set to 3. Then the value 3 is displayed 2 times...
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks. Ankit and Punit.
[ December 22, 2008: Message edited by: Abhi vijay ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance