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.
A, B, and C are working together on a project. A can complete the project alone in 15 days, B in 20 days, and C in 30 days. On the first day, A, B, and ...
The speed of a train is 36 km/hr and it takes 12 seconds to cross a man. Find the time taken by train to cross a platform if the ratio of the length of ...
- Maya has to go to the airport. If she drives at 36 km/h, she arrives 1.5 hours late. If she drives at 60 km/h, she reaches 45 minutes early. What is the di...
How much time a train 100 metres long running at the speed of 72 km/hr take to cross a bridge 180 metres in length?
- Two cyclists, Aman and Raj, start at the same time from the same place and travel to a common destination. If their speeds are in the ratio 7:5 and Raj tak...
A train has to cover a distance of 350 km in 25 hours. If it covers half journey in 3/5th time, then the speed of covering the remaining dist...
If a bicycle can cover 65 km in 10 hours while a car can cover 90 km in 5 hours then find the average speed of car.
Saket and Rakesh are brothers whose shops are located at a distance of 10km and 12km respectively from their home. Both of them start at the same time f...
A can complete a piece of work in 12 days, and B can complete the same work in 16 days. They work together for 4 days, and then A leaves. How many more ...
Two classmates R and S stand at points U and V respectively. R starts toward V from U at a speed of (x – 5) km/h. After 3 hours, S starts toward U fro...