• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inheritance

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Devaka Cooray's Simulator( made some modifications)



The answer is Demo3 Demo3.
But how is super.s returning the same value.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Ankit and Punit.
[ December 22, 2008: Message edited by: Abhi vijay ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic