# Dynamic Method Selection Source: https://wisconsin.twango.dev/fa24-cs300/u2-oop/inheritance/dynamic-method-selection # Dynamic Method Selection Dynamic Method Selection (DMS) is a process in Java that determines which method to call at runtime based on the dynamic type of the object. This process is essential for polymorphism, where a single method call can execute different behaviors based on the object's type. ### Entry Point: Compiling the Code - The process begins when a user compiles Java code. At this point, the Java compiler initiates several checks to validate the code and set up necessary conditions for method selection. ### Compile-Time Selection - The compiler performs various checks to ensure the code is syntactically correct and logically consistent for method execution. This stage is crucial for establishing a "locked" method signature that dictates which method should be invoked, based on static analysis. - Syntax Error Check: - The first step is to verify if the code contains any syntax errors. If syntax issues are found, a Compile Error is raised, and the compilation process stops. - Checking for Casts: - If no syntax errors are found, the compiler checks if there are any type casts in the code, as type casting affects which method might be called. - If casts are present, the compiler verifies whether these casts fall anywhere within the inheritance hierarchy to prevent illegal casting. - If the casts are outside inheritance, a Compile Error is triggered. - If the casts are valid, the compiler proceeds to the next step. - Static Signature Verification: - The compiler then checks whether the static (compile-time) method signature matches a known method. - If the exact method signature is found, the compiler locks this signature, creating a "locked" method that the program intends to call. - If the exact static signature does not exist, the compiler checks for a similar method signature in any superclass. - If no compatible superclass method exists, a Compile Error occurs. - If a matching signature is found in a superclass, the method is locked, and the process moves to the runtime phase. ### Run-Time Selection - After a successful compile-time selection, the process enters the runtime environment, where dynamic checks are performed. These checks ensure that the actual objects and methods align with the locked compile-time method signature. - Checking for Casts: - Similar to the compile-time phase, the runtime environment first checks if any casts exist within the code. - If casts are present, the runtime validates whether they conform to the inheritance hierarchy. - If the runtime cast does not follow inheritance, a Runtime Error occurs. (i.e. an invalid cast down the inheritance tree) - If the cast is valid, the runtime proceeds. - Static Method Determination: - The runtime then verifies whether the method involved is static. Static methods, which are resolved at compile-time, do not participate in DMS, so they are executed as locked. If the method is static, the code proceeds to Run with Locked Method Signature. - Resolving Dynamic Types: - For non-static methods, the runtime determines the dynamic types of objects to see if the locked compile-time signature matches the dynamic (runtime) signature. - Matching Dynamic Signature with Locked Signature: - If the dynamic method signature matches the compile-time locked signature, the program proceeds to Run with Dynamic Method, completing the DMS process. - If there’s no direct match with the locked signature, the runtime checks if the superclass method matches the locked signature. - If a superclass match is found, the process re-evaluates the dynamic types to ensure compatibility, then rechecks the signature match. - If the superclass fails to match the locked signature, the method defaults to the Run with Locked Method Signature. ## Summary ```mermaid graph TB Entry>User Compiles Java Code] CE[[Compile Error]] RE[[Runtime Error]] RD[[Run with Dynamic Method]] RLC[[Run with Locked Method Signature]] subgraph Compile Time Selection Syntax[Syntax Error Check] CCasts[Cast Exists?] ValidCCasts[Cast Within Inheritance?] Static[Static Signature Exists?] SuperStatic[Static Signature in Superclass?] end LockC{{Lock Method Signature}} subgraph Run Time Selection RCasts[Cast Exists?] ValidRCasts[Cast Follows Inheritance?] IsStaticMethod[Is Static-Class Method?] DynamicTypes{{Resolve Dynamic Types}} ReDynamicTypes{{Override to Dynamic Superclass}} DynamicSignatureExists[Dynamic Match with Locked Signature?] SuperDynamicSignatureExists[Superclass Match with Locked Signature?] end style Entry stroke:#800080,stroke-width:2px style LockC stroke:#FFA500,stroke-width:2px style CE stroke:#800000,stroke-width:2px style RE stroke:#800000,stroke-width:2px style RD stroke:#006400,stroke-width:2px style RLC stroke:#006400,stroke-width:2px style ReDynamicTypes stroke:#006400,stroke-width:2px Entry --> Syntax Syntax -- Yes --> CE Syntax -- No --> CCasts CCasts -- Yes --> ValidCCasts ValidCCasts -- Yes --> Static ValidCCasts -- No --> CE CCasts -- No --> Static Static -- Yes --> LockC Static -- No --> SuperStatic SuperStatic -- No --> CE SuperStatic -- Yes --> LockC LockC --> RCasts RCasts -- Yes --> ValidRCasts ValidRCasts -- Yes --> IsStaticMethod ValidRCasts -- No --> RE RCasts -- No --> IsStaticMethod IsStaticMethod -- Yes --> RLC IsStaticMethod -- No --> DynamicTypes DynamicTypes --> DynamicSignatureExists DynamicSignatureExists -- Yes --> RD DynamicSignatureExists -- No --> SuperDynamicSignatureExists SuperDynamicSignatureExists -- Yes --> ReDynamicTypes ReDynamicTypes --> DynamicSignatureExists SuperDynamicSignatureExists -- No --> RLC ``` ## Source The existing course note was retained and normalized during the merge. Related: [[u2-oop/inheritance/index|Topic index]].