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.
When did the British Government announce the August Offer?
Which country currently holds the top position in the latest (Oct 2024) FIFA rankings?
In Exercise Siyom Prahar, which set of drone roles was demonstrated?
What is the primary objective of the NPS Vatsalya scheme launched by the Finance Minister?
India’s first ambulance for street animals has been launched in ______________ which is started by the Blue Cross of India in collaboration with t...
The data released by the NSO showed that the number of new subscribers adopting the National Pension System (NPS) among corporate employees declined by ...
On 9 November,2021 Koneru Ramakrishna Rao passed away. He was a ____ by profession?
Which of the following bankers is one of the lead bankers for the transaction involved in the initial public offer (IPO) of the Indian Renewable Energy ...
Who will be awarded the 2023 SASTRA Ramanujan Prize for outstanding contributions in mathematics?
What is the deadline for the census enumeration phase to be completed in India, according to the government?