Posts

Showing posts from July, 2021

Virtual methods in Delphi

Image
Virtual methods enable subtype polymorphism - in other words, they allow implementing different behavior in descendant classes. That means if you have a base TShape class with a virtual Paint method, and several descendant classes like TRectangle , TCircle and TTriangle , then each of those subclasses can implement a different Paint method to appropriately paint itself. You can call the Paint method on any shape instance without needing to know which kind it is, and it will be correctly painted. Static vs dynamic dispatch Virtual methods can achieve polymorphism via a mechanism called dynamic dispatching at runtime, while non-virtual methods will be statically dispatched (bound) during compilation. Polymorphism cannot be achieved by statically bound methods—in other words, when you call a statically bound method, you will call the same implementation for all descendant classes. Static dispatching means that the compiler will resolve the method address at compile time, and will