Mahmoud Metwally<br />Preparing for SCJP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
Originally posted by sweety sinha:
static init block executed at class loading time
instance init block runs after call to super constructor
when multiple init block of a single type occur in a class they run in order from top down
i hope this will help you
Mahmoud Metwally<br />Preparing for SCJP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
SCJP 5.0
SCJP 5.0
My example shows that this statement is wrong. If it were true, the output had to be 1. 3. 2. 4. !!it seems that instance variables are given their assigned values after the call to super() and before the instance init blocks run.
SCJP 5 (98%) - SCBCD 5 (98%)
SCJP 5.0
SCJP 5 (98%) - SCBCD 5 (98%)
is definitely wrong in general. It is correct in your exampleit seems that instance variables are given their assigned values after the call to super() and before the instance init blocks run
SCJP 5 (98%) - SCBCD 5 (98%)
Originally posted by Ralph Jaus:
Hi Keith, the statement is definitely wrong in general. It is correct in your example
int collarSize = 3;
{ collarSize = 10; }
because there is at first a variable declaration/initialization and afterwards the init block. And that's just the order the compiler takes.
If your int block would be followed by a declaration/initialization of a variable, say
int i = 7;
it would be processed next, that means after your init block !!
SCJP 5.0
Mahmoud Metwally<br />Preparing for SCJP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
SCJP 5 (98%) - SCBCD 5 (98%)
Originally posted by Ralph Jaus:
The output shown here is due to overriding the method f. Since you instantiate an object of the subclass even in variable initialization, initialization blocks and constructors the overriden method is used.
[ July 17, 2008: Message edited by: Ralph Jaus ]
Mahmoud Metwally<br />Preparing for SCJP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
SCJP 5 (98%) - SCBCD 5 (98%)
Originally posted by Ralph Jaus:
Since you instantiate an object of the subclass even in variable initialization, initialization blocks and constructors of the super class the overriden method is called.
But what exactly are you confused about ?
[ July 17, 2008: Message edited by: Ralph Jaus ]
Mahmoud Metwally<br />Preparing for SCJP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
SCJP 5 (98%) - SCBCD 5 (98%)
Mahmoud Metwally<br />Preparing for SCJP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
Mahmoud Metwally<br />Preparing for SCJP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
Originally posted by Ralph Jaus:
Only methods can be overriden, not variables, initialization blocks or constructors.
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
The first time it appears because of the statement "int zA = g(99);" in class Animal.Why 99_SUBClass did appear (Twice)?
The second time it appears because of the statement "int z = g(99);" in class DogVer3.2. If these artefacts in the super class (Animal) use a method of the super class (g) that is overriden in a sub class (DogVer3), which method is called during runtime when an instance of the sub class is created ? -> the overriding method (that means g in DogVer3).
SCJP 5 (98%) - SCBCD 5 (98%)
SCJP 5 (98%) - SCBCD 5 (98%)
Mahmoud Metwally<br />Preparing for SCJP...<br /> <br />"Try not to become a man of success but a man of value" - Albert Einstein
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Originally posted by Raphael Rabadan:
Hi Nabila, let me try to explain you..
The subclass is not taking place, its the superclass, like it should, and like you are thinking :-) But the thing here is, the method of superclass was overrided by the subclass, so, when running the superclass initialization block, its using the subclass method instead of the superclass one, and making you think that its the subclass initialization.
Understand now? If not, I'll try to make it more clear later..
[ July 17, 2008: Message edited by: Raphael Rabadan ]
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Yes, your understanding of Raphaels explanation is correct.I suppose that's what you meant.
Can you please show the whole code.Dog1 d=new Dog1(); OR Animal d=new Dog1();d.f(1);
In both the cases, Base method is called and not the parent class
SCJP 5 (98%) - SCBCD 5 (98%)
Originally posted by Nabila Mohammad:
...
The only conclusion I came up with is..
Dog1 d=new Dog1(); OR Animal d=new Dog1();
d.f(1);
In both the cases, Base method is called and not the parent class.
So if you have a base class object and an overriden method then the base class will be executed.
Am I correct or Am i mixing something entirely differnt?
...
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
1) How was the instance varibles of the Super class getting initilaised before running the super class constructor and same goes for the Base constructor.
2)While priniting out the value of "m" - it gave a compiler error
However after commenting it - Animal.m and DogVer.m both were executed and were given default values even though m wasnt declared or initialised at that time.
3) And why was "m" given a compile time errr when DogVer3.m was running.
-For some weird reason Instatic SUPERwas printed twice
-While callng method fstatic() of SUper class - it gives the result 5_SUPER_STATIC_VAR and not 55_SUPER_STATIC_VAR -OR- 5_SUBCLASS_STATIc_VAR
- after fstatic(), it skipped the static init block of superclass and moved on tothe static init block of Sub Class..
The future belongs to those who believe in the beauty of their dreams.Dream BIG!
These are the worst of times and these are the best of times. And this is the best tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|