• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance  RSS feed

 
Firoj Raj Rai
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we're creating the object of a sub class is it that the super class is loaded automatically?
Why is the Instance block of Sub processed before the Static block?

The output is:

Static block in Super
Instance block in Super
Instance block in Sub
Static block in Sub
Main in Sub
 
Kaustubh G Sharma
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch.. you'll get more response, if you'll move your in correct forum. Here if you see in your super static block you're creating an instance on sub class, I think that's the reason you're getting below result.

 
dhayanandhan Pandi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Firoj,

If you want to exectue super class first the following format

class Super {
static {
System.out.println("Static Block in Super");
}
{
System.out.println("Instance Block in Super");
}
}
class Sub extends Super{
{
System.out.println("Instance block in Sub");
}
static {
System.out.println("Static block in Sub");
}
public static void main(String []arg){
Super s = new Super();
System.out.println("Main in Sub");
}

}

Output:

Static Block in Super
Static block in Sub
Instance Block in Super
Main in Sub



Otherwise you want to exectue sub class first the following format

class Super {
static {
System.out.println("Static Block in Super");
}
{
System.out.println("Instance Block in Super");
}
}
class Sub extends Super{
{
System.out.println("Instance block in Sub");
}
static {
System.out.println("Static block in Sub");
}
public static void main(String []arg){
Sub s = new Sub ();
System.out.println("Main in Sub");
}

}

Output:

Static Block in Super
Static block in Sub
Instance Block in Super
Instance block in Sub
Main in Sub


Thanks&Regards,
Dhaya
 
Josh Rowe
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure why you are using static initializers with this code, because there is an easier way to show what you're seeing here. The simple fact is that when a subclass constructor is called it implicitly calls or invokes its superclass constructor all the way up the Java class hierarchy. That is the reason you are seeing the super class one before the subclass being printed. The static initializers, on the other hand, are called when the class is loaded. This is done in sequence, unless some other syntax modifies the flow of control. Apart from the constructor chaining, I couldn't tell you why you're getting the output you are getting. I wrote code similar to yours and I couldn't get it to compile because it doesn't like the static block declaration in the inner class. If you haven't already, copy the code exactly from your program into the thread.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!