Java : Constructor, static block, Inheritance and their connection with each other

Jyoti
3 min readApr 19, 2022
Java

Whenever I heard of Java Object Oriented Programming concepts all that come to my mind is definitions and basic syntax of abstraction, encapsulation, inheritance, polymorphism, but once I started writing codes for it, there I come to know many permutations and combination of oops concepts that just blow off my mind. It seems complicated at first glance but once you get the logic behind it, it all makes sense.

First of all lets come to constructor, the image come to your mind when you read constructor is :

  1. Its a special function that is invoked automatically when object of that class is created.
  2. It is used to initialize the state of an object
  3. Its name is same as name of class
  4. It can have 0 or any number of parameters
  5. It doesn’t have any return type

Something like this:

class Parent{Parent(){System.out.println(“ i am in parents constructor “);}}

But when a child class inherits the parent class, and whenever you create the object of child class, we know constructor of child class will run automatically, but you know what, the constructor of parent class is called and run first automatically before the child class constructor runs.

class Parent{Parent(){System.out.println(“ i am in parents constructor “);}
}
class Child extends Parent{Child (){
System.out.println(“ i am in child constructor “);
}
}
class Main{
public static void main(String[] args) {
Child c1= new Child();
}
}

output will be:

i am in parents constructor

i am in child constructor

But wait, if there is static block present in parent, then as we know static blocks are resolved at compile time and runs only once, so whenever first object of that class is created then static block runs first and then the constructor.

class Parent{static {System.out.println(“ i am in parent static block “);
}
Parent(){System.out.println(“ i am in parents constructor “);}}class Main{public static void main(String[] args) {
Parent p= new Parent();
}
}

output will be :

i am in parent static block

i am in parents constructor

Okay this seems good. But what when a static block is present in parent class and child class as well. So at that time, if we create object of child class and “no parent class object is created yet” (because static block runs only once) , then the static block of parent will run first, then static block of child, then constructor of parent then constructor of child.

class Parent{static {System.out.println(“ i am in parent static block “);
}
Parent(){System.out.println(“ i am in parents constructor “);}}class Child extends Parent{static {System.out.println(“ i am in child static block “);
}
Child (){
System.out.println(“ i am in child constructor “);
}
}class Main {public static void main(String[] args) {Child c1= new Child();}}

output will be :

i am in parent static block
i am in child static block
i am in parents constructor
i am in child constructor

Some extra things about constructor:

  1. Cannot be abstract, final, static
  2. Modifiers public, protected and, private are allowed with constructors. default is declared when no modifier name specified explicitly. (info)

--

--

Jyoti

Explorer, Curious and a head full of questions and thoughts.