Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
This is why familiarity with C ++
is useful (=
The bottom line is: an object-type is loaded into memory somewhere (not an instance with data, but a representation of the type), in which the method code is physically located. Each class instance has a reference to its own object -type.When the type of an object reference matches its type, everything is simple: we know that the method can be found directly.Perspectives for compiler optimization open up: substitution of a specific method address or even inlining (when a method call is replaced directly with code from a method, so as not to spend cycles on passing parameters). This is early binding... If
we have a polymorphiccall (a reference variable can point to a child object that can have an override of the parent method), then you need to know exactly what type the object is in order to call the appropriate method definition from the desired type. Alas, we learn this only at runtime, so the compiler will not help optimize this process. This is late binding .
In many C-like languages (C++, C#, etc.), for the sake of performance, early binding is used by default: polymorphic methods must be explicitly marked by the programmer with a keyword virtual
(otherwise the method will be called from the reference variable type, not polymorphically), in Java but for the sake of readability of the code is used laterdefault binding: all methods are implicitly "virtual", so the quality of the binding is at the mercy of the compiler.
Of course, it is possible to mark a method with the keyword final
, which will prevent the method from being overridden in descendants, i.e. the compiler will have all the authority to hard drive the necessary address of the method into the call point without fear, because there simply cannot be alternative versions of the method in the heirs.
(C++11 also has the keyword final
, while C# has the keyword sealed
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question