There are mainly three rules for the instance initializer block.
They are as follows:
1. The instance initializer block is created when instance of the class is created. 2. The instance initializer block is invoked after the parent class constructor is invoked (i.e. after super() constructor call). 3. The instance initializer block comes in the order in which they appear. 4. Note: The java compiler copies the code of instance initializer block in every constructor after super
Default Constructor:
f a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared. Example public class Dog { } will automatically be modified(by adding default constructor) as follows public class Dog{ public Dog() {
} } and when you create it's object Dog myDog = new Dog(); this default constructor is invoked. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor
The constructor chaining is the example of inheritance in java, which is the key attributes of OOPs technology. A first line of the each constructor in your java class must be either ? 1 2 this(); super(); If you does not put any constructor call in first line of class's constructor, the compiler automatically put the no arguments super() i.e. super class constructor call. Java constructor chaining Constructor chaining occurs when a class inherits another class i.e. in inheritance, as in inheritance sub class inherits the properties of super class. Both the super and sub class may have constructor methods, when an object of sub class is created it's constructor is invoked it initializes sub class attributes, now super class constructor needs to be invoked, to achieve this java provides a super keyword through which we can pass arguments to super class constructor. For more understanding see constructor chaining example: class GrandParent { int a;
GrandParent(int a) { this.a = a; } }
class Parent extends GrandParent { int b;
Parent(int a, int b) { super(a); this.b = b; }
void show() { System.out.println("GrandParent's a = " + a); System.out.println("Parent's b = " + b); } }
class Child { public static void main(String[] args) { Parent object = new Parent(8, 9); object.show(); } }