Question
Which of the following is an example of Polymorphism in
Object-Oriented Programming?Solution
Polymorphism is one of the fundamental principles of Object-Oriented Programming (OOP) and refers to the ability of a function, method, or operator to act on different types of data. The word “polymorphism” literally means "many forms," and in OOP, it allows objects of different classes to be treated as objects of a common superclass. The most common types of polymorphism are method overriding and method overloading .
- Why It Is Important: Polymorphism enables flexibility and scalability in code. It allows a method to be used with different types of objects, which reduces code redundancy and promotes code reusability. For example, a single method in a base class could be used for different derived classes, as long as the derived classes implement the same method.
- Real-World Example: Consider an abstract class Animal with a method sound() . The subclasses Dog and Cat override the sound() method. When the method sound() is called, it produces different results based on the object type ( Dog or Cat ), demonstrating polymorphism.
- A base class object cannot access the methods of a derived class: This is a misunderstanding of inheritance. A base class object cannot access the methods specific to a derived class unless the object is of the derived type.
- A derived class can directly modify private attributes of the base class: This violates the principle of encapsulation . A derived class cannot directly access private members of the base class unless accessors are provided.
- Â An object can only interact with its own type: This contradicts polymorphism, as polymorphism allows objects to interact with different types through method overriding or overloading.
- Data members of a class are always public: This is incorrect. Data members can be public, private, or protected, but OOP encourages encapsulation, where data members are often private, not public.
The ratio of the boat's speed in still water to the speed of the stream is 10:2. The boat takes time in the ratio 11:15 to travel...
The downstream speed of a boat is 48 km/hr, while its upstream speed is 32 km/hr. The boat takes 10 hours to travel a distance of (p + 40) km downstream...
A boat, whose speed in still water is 25% more than the speed of the current, covers 810 km downstream in _____ hours. The speed of the boat in still w...
A boat goes 65 km in upstream in 5 hours and takes 4 hours to complete a distance of 20 km in downstream. Find the speed of boat in still water.
The speed of current is 29 km/hr. If the speed of the boat in still water had been 17 km/hr more than the original speed then it would take 6.2 hours to...
If a man rows at the rate of 26 kmph in still water and his rate against the current is 3 kmph, then the man’s rate along the current is: Â
...Ratio of speed of boat in downstream and speed of stream is 5:2, if speed of current is 4 km/hr, then find distance travelled (in km) upstream in 6 hours.
A boat moves downstream at 45 km/h, which is (25/10) m/s faster than its upstream speed. If it covers (D + 24) km downstream and (D - 36) km upstream in...
The speed of current is 40 km/hr. If the speed of the boat in still water had been 10 km/hr more than the original speed then it would take 5.5 hours to...
A boat can cover 188 km in downstream in 4 hours and 140 km in upstream in 7 hours. Find the speed of boat in still water.