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 right prism has a square base with side of base 4 cm and the height of prism is 9 cm. The prism is cut in three parts of equal heights by two planes p...
If 6A = 4B = 9C; What is A : B : C?
- If, ‘x’ varies directly as ‘y’, and when x = 56, y = 28, then find the value of ‘x’, when ‘y’ = 40.
- ‘A’, ‘B’ and ‘C’ divide a certain sum of money among themselves. The average of the amount with them is Rs. 4520. Share of ‘A’ is 10(2/3)% ...
Rs. 18,000 is divided among 'X', 'Y', and 'Z' such that one-fourth of X’s share is equal to 20% of Y’s share which is equal to one-sixth of Z’s sh...
The number of students in class IX and class X is 42 and 45, respectively. The ratio of the number of boys to girls in classes is IX and X is 9: 5 and 8...
- Rs. 36000 is shared among 'Pawan', 'Qureshi' and 'Raju' such that 'Pawan' receives (3/5) of what 'Qureshi' gets and (3/2) times of what 'Raju' gets. What i...
The ratio of A’s salary to B’s salary is 5 : 7. Each of them gets an increment of Rs 3,000 per month, and the new ratio of their salaries becomes 8 ...
Number of faculties in Physics and Biology in an institute is in the ratio of 5:7 respectively. If 45 more faculties join Physics, while 24 more faculti...
The proportion of books sold by Sita on Monday to those on Tuesday stands at 8:5. Sita's book sales on Wednesday exceed Tuesday's by 34, while Thursday'...