An abstract class can have an initialisation block.
Please explain the reason behind this as I am not clear. Initialisation blocks are executed when an instance of a class is created. We can not have an instance of an abstract class. Then why are abstract classes allowed to have initialisation blocks.
Below mention example (Source:http://www.examlab.tk/) compiles fine. I was thinking that the answer would be "Compilation fails".
We can not have an instance of an abstract class. Then why are abstract classes allowed to have initialisation blocks.
Of course, you *can* have an instance of the class. You just can't instantiate it directly -- you have to instantiate a concrete subclass. A concrete subclass IS-A instance of the abstract class. (The initialization block is executed as part of the subclass' instantiation)
Henry [ November 12, 2008: Message edited by: Henry Wong ]
Looks like Interfaces are not allowed to have static initialisation blocks. We can create their instance also through their subclasses. Then for interface, why it is not allowed to have an initialisation block.
I tried to compile the following program. It is giving me errors.