aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance again 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 again" Watch "Inheritance again" New topic
Author

Inheritance again

Aparna Shatdarsanam
Greenhorn

Joined: Nov 28, 2002
Posts: 9
Can someone explain me the following piece of code and the output that's got !
Any further tips to solve such kind of questions is highly appreciable.
class SuperclassA
{
public SuperclassA()
{
System.out.println("SuperclassA");
}
}
class SubclassB extends SuperclassA
{
public SubclassB()
{
this(3);
System.out.println("Default Empty SubclassB");
}
public SubclassB(int i)
{
System.out.println("Parameter Cnstr SubclassB");
value =i;
}
{
System.out.println("Hakuna Matata !");
}
private int value = crapp();
private int crapp()
{
System.out.println("Hakuna Matata 12345");
return 1;
}
}
public class Testing123
{
public static void main(String[]ars)
{
new SubclassB();
}
}
Thanks,
Aparna.
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Wow, that's pretty weird. I got as far as SuperclassA being printed out... which occurs in the no-arg constructor for Superclass A which gets called implicitly... but then after that i do not know what happens.
I thought i knew what would happen, but when i ran it i did not understand why it printed what it did.

thanks for confusing me!


giddee up
Aparna Shatdarsanam
Greenhorn

Joined: Nov 28, 2002
Posts: 9
But I thought the no-arg constructor of the superclassA would not be called as we explicitly call the constructor through this(3) ...
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
Aparna, I will try to give an explanation

When a new subclass object is created , as in step one (marked as (1)),
1) The no-args constructor of subclass has this(3) as its first statement. There must be a this or super. If either is not there, then a super() is inserted by the compiler.
2) As there is a this(3), the subclassB constuctor which takes int is called. As explained above, since there is no this or super in SubClassB(int) constructor, a super() is inserted by the compiler. So this will call the no args constructor of SuperclassA. Hence the first line of print is SuperclassA
3) If there are any SuperclassA instance variables, they are set before giving back the control to the subclass.
4) in the subclass, (4) is the code for instance intializer which is executed first and then as explained above, the instance variables for the class are set. Thats why value = crapp() is called.
5) after the instance variable is set , now the control goes back to the place from where the super constructor was called. That is (6).
6) After line (6) is executed, the value variable is reset and the control goes back to SubclassB no-args constructor. There line (7) is printed.
Remember these points
1) Instance intializers , then instance variables, then constructors are executed when a class is instantiated.
Ranchers, Please correct me if I am wrong.
Thanks and
Hope this one stimulates your mind for some more discussion
Sri
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Aha, so the the first instance of Subclass does not call the superclass constructor (due to the call of this(3) within the initial constructor body), but then the second constructor of the Subclass is called, ironically by this(3), and it has no call of this in it so the superclass constructor is implicitly called.
Thanks Sri!
now, to understand the rest of it...the instance variables, initialisers, and calls, hmm.
Aparna Shatdarsanam
Greenhorn

Joined: Nov 28, 2002
Posts: 9
Thanks Sri for such detailed explanation.
The point that I learnt here is even in the sub-class constructors that take arguments, call to super() would invoke superclass's no-arg constructor.
Coming to the instance variables, what wud be printed at (a) ?
class SuperclassA {public SuperclassA(){//(3)System.out.println("SuperclassA");}}class SubclassB extends SuperclassA{public SubclassB(){ //(2)this(3);System.out.println("Default Empty SubclassB"); //(7)}public SubclassB(int i){System.out.println("Parameter Cnstr SubclassB");//(6)
value =i;
System.out.println("Value of value is" + value);//(a)}
//instance intializer {//(4)System.out.println("Hakuna Matata !");}private int value = crapp(); //(5)private int crapp(){System.out.println("Hakuna Matata 12345");return 1;}}public class Testing123{public static void main(String[]ars){new SubclassB(); //(1)}}
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
Sri Sri,
I just want to add some thing more to your comments.
The order of execution is
a) calling the super class constructors,
b) Static initialization blocks or static variables in the order they appear in source code
c) instance initialization blocks or instance variables in the order they appear in the souce code.
d) The constructor of the current class.

These rules are applied for all the classes in the hierarchy.
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366


I just want to add some thing more to your comments.
The order of execution is
a) calling the super class constructors,
b) Static initialization blocks or static variables in the order they appear in source code
c) instance initialization blocks or instance variables in the order they appear in the souce code.
d) The constructor of the current class.

These rules are applied for all the classes in the hierarchy.


I beg to differ on a few points.
You have given the order as Superclass constructors and then static initializers.
I dont think it is the case.
Static Intializers do not have anything to do with object instantiation. They run once as and when the program is executed. For example

The result is first the code in static initializer is printed , then the superclass constructor is printed.
Coming to the order of instance initializer and variable initialization code, i didnt quite get what you were trying to tell.
I have an extension of the above code here which shows instance initializer code runs first before the initialization of the instance variable even though instance initializer code is after the variable initialization.


The output of the program is

Hello from subclass static intializer
Hello
Instance initializer code
The value of i is initialized here : 6

I remember that you cannot call any instance methods unless the super constructor is completed
Thanks
Sri
Todd Killingsworth
Greenhorn

Joined: Jan 30, 2002
Posts: 28
Static initializers come first, and are executed when the class is first loaded into memory - Instance not required.
Change the first example to make the 2 initializers static - and make method crapp() static. Their output preceeds the superclass constructor.
i.e.
Hakuna Matata!
Hakuna Matata 12345
SuperclassA
.
.
.
Todd Killingsworth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance again