~/ learn/ comp-308/ cards/ Polymorphism pitfalls & constructors
1 of 5

The trap: a base constructor calling an overridable method

The trap: a base constructor calling an overridable method

Answer

class Glyph { Glyph() { draw(); } void draw() { System.out.println("Glyph.draw"); } }

`Glyph()` calls the overridable `draw()`. In a subclass that overrides `draw()`, the subclass version runs before the subclass's fields are initialized — reading default (zero) values.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/89c90851-296f-4d55-beaf-06ed29eff7a5/flashcard utf-8 LF