~/ 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/4921f4ce-0cf6-45be-82ed-9433d4ccdf22/flashcard utf-8 LF