Question
Which of the following scenarios illustrates runtime
polymorphism?Solution
Runtime polymorphism (or dynamic method dispatch) occurs when a method's implementation is determined at runtime. This is achieved using method overriding in OOP. A derived class overrides a method of the base class, and the method to execute is determined by the type of the object being referenced at runtime. Example: class Animal { ┬а ┬а void sound() { ┬а ┬а ┬а ┬а System.out.println("Animal makes a sound"); ┬а ┬а }} class Dog extends Animal { ┬а ┬а void sound() { ┬а ┬а ┬а ┬а System.out.println("Dog barks"); ┬а ┬а }} public class Main { ┬а ┬а public static void main(String[] args) { ┬а ┬а ┬а ┬а Animal animal = new Dog();┬а // Base class reference pointing to a derived class object ┬а ┬а ┬а ┬а animal.sound();┬а ┬а ┬а ┬а ┬а ┬а // Outputs: Dog barks ┬а ┬а }} Why Other Options Are Incorrect: 1. Method with the same name but different parameters within the same class: This is compile-time polymorphism (method overloading), not runtime polymorphism. 2. Default arguments in methods: Default arguments provide flexibility but are unrelated to polymorphism. They simplify method calls without changing the behavior based on the objectтАЩs runtime type. 3. Static methods for shared functionality: Static methods are class-level and cannot participate in runtime polymorphism, as they are resolved at compile time. 4. Constructor with a parameter list in the derived class: Constructors are not polymorphic as they do not inherit or override behavior in OOP.
' рдирд┐рд░реНрджреЛрд╖ ' рд╢рдмреНрдж рдореЗрдВ рдкреНрд░рдпреБрдХреНрдд рдЙрдкрд╕рд░реНрдЧ рд╡ рдореВрд▓рд╢рдмреНрдж рд╣реИ-
рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдкреНрд░рддреНрдпреЗрдХ рд╢рдмреНрдж рдХреЗ рд▓рд┐рдП рдЙрд╕рдХреЗ рдиреАрдЪреЗ рджрд┐рдП рдЧрдП рд╡рд┐рдХрд▓я┐╜...
рдиреАрдЪреЗ рдкреНрд░рддреНрдпреЗрдХ рд╡рд░реНрдЧ рдореЗрдВ рджрд┐рдП рд╡рд┐рдХрд▓реНрдкреЛрдВ рдореЗрдВ рд╕реЗ ┬а рддрджреНрднрд╡ рд╢рдмреНя┐╜...
'рд╕реБрд░' рдХрд┐рд╕рдХрд╛ рдкрд░реНрдпрд╛рдпрд╡рд╛рдЪреА рд╣реИ?
рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдкреНрд░рддреНрдпреЗрдХ рд╢рдмреНрдж рдХреЗ рд▓рд┐рдП рдЙрд╕рдХреЗ рдиреАрдЪреЗ рджрд┐рдП рдЧрдП рд╡рд┐рдХрд▓я┐╜...
рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдореЗрдВ рд╕реЗ ' рд╡рд┐рдЪрд┐рддреНрд░ ' рдХрд╛ рдкрд░реНрдпрд╛рдпрд╡рд╛рдЪреА рдХреНрдпрд╛ рд╣реЛрдЧрд╛ ?
'рд╣реЛрд╢рд┐рдпрд╛рд░'
рдиреАрдЪреЗ рджрд┐рдП рдЧрдП рд╡рд┐рдХрд▓реНрдкреЛрдВ рдореЗрдВ рд╕реЗ рд╕рджреИрд╡ рдмрд╣реБрд╡рдЪрди рд░рд╣рдиреЗ рд╡рд╛рд▓реЗ рд╕рдВрдЬреН...
рдзреЛрдмреА рдХрд╛ рдХреБрддреНрддрд╛ рди рдШрд░ рдХрд╛ рди рдШрд╛рдЯ рдХрд╛' рд▓реЛрдХреЛрдХреНрддрд┐ рдХрд╛ рд╕рд╣реА рдЕрд░реНрде рд╣реИ
рдирд┐рдореНрдирд▓рд┐рдЦрд┐рдд рдкреНрд░рддреНрдпреЗрдХ рдкреНрд░рд╢реНрди рдореЗрдВ рджрд┐рдП рдЧрдП рд╢рдмреНрдж рдХреЗ рд╕рдорд╛рдирд╛рд░я┐╜...