Static vs. Virtual Methods
The definitions of static and virtual methods are below. You'll probably want to refer back to them several times: static methods: the default; if redefined by a descendant, parameter lists and type returned can differ at each definition; calls resolved at COMPILE-TIME virtual methods: virtual directive required in the method heading in the interface part; virtual method headings must be identical in the ancestor and in all descendant object types; calls resolved at RUN-TIME; objects containing virtual methods must be explicitly initialized at run time by . . .
For example, there are 2 methods for ComputeArea (Square inherits it from Rectangle). That is, you can add capabilites to a descendent object type without access to the ancestor's source statements. A simple example of polymorphism is used here. Note, this assignment is not particularly useful since F1 does not capture all the information about circle C1--so F1 is not really a circle. It is always safe to make a method a virtual method so that polymorphism can be used with late binding. al methods known as constructors; an object can have several constructors, which may be inherited; constructors may not be virtual You won't really understand the difference between static and virtual methods until you have seen several examples. When the "ComputeArea" message is sent, which method is used? If the message is sent to MySquare, then the method for the square type is used. Since C1 contains at least the same data members as F1, the assignment will completely define F1. So even though static methods use less memory and execution time than virtual methods, unless space and time efficiency are essential, your objects should use virtual methods instead of static methods. } Similarly, either F1 or C1 could be an actual parameter to a procedure whose corresponding formal parameter is of type Figure. Trace through the code paying attention to the comments. ----------------------------------------------------------------------------- Now consider the driver program, CliFigFa.
Common topics in this essay:
, BINDING EXCITING, F1 C1, Rectangle ComputeArea, C1 Circle, BINDING Virtual, F1 ALLOWED, CliFigFapas Trace, F1 Note, virtual methods, C1--so F1, late binding, message sent, data f1, static methods, contains data f1, object type, static virtual, polymorphism late, object types, calls resolved, polymorphism late binding, static virtual methods, virtual methods required, |