We can initialize an abstract class through initialization of a concrete sub class. Therefore, should be able to call the abstract methods of an abstract class only after the initialization of a subclass However, the following initialization block in abstract class Mammal can call an abstract method before the initialization of the sub child class.
If you call the constructor of Platypus with new Platypus(), it outputs: "yummy!", "Mammal constructor", "Platypus constructor" in the same order. I thought an instance of a class gets created when the constructor is called. But the initialization block in Mammal is able to call the method before Platypus initialization.
abstract class Mammal {
{
System.out.println(chew());
}
abstract CharSequence chew();
public Mammal() {
System.out.println("Mammal constructor");
}
}
public class Platypus extends Mammal {
public Platypus() {
System.out.println("Platypus constructor");
}
String chew() {
return "yummy!";
}
}