Java program to implement instance initializer block with superclass
belongs to collection: Java Instance Initializer Block Programs
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
belongs to collection: Java Instance Initializer Block Programs
total answers (1)
Program/Source Code:
The source code to implement instance initializer block with superclass is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created three classes A, B, and Main. Class A contains an Instance Initializer Block, a constructor. Then we inherited the class A into B. The A and B, Both class contains IIBs.
In the above program, it looks like IIB is calling before the constructor. But it is not true. IIB gets called when an object is created. Java compiler copies the IIB in the constructor after the first statement super(). So constructor calls the IIB.
The Main class contains a method main(). The main() method is the entry point for the program, here we created the object of the B class, it calls B class IIB and constructor, and B class constructor calls A class IIB and constructor.