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.
In a certain code language, 'KITCHEN' is written as 'LHUBIDO', and 'HUSBAND' is written as 'ITTABME'. How will 'ELEMENT' be written in that language?
In a certain code language,
R + T means ‘R is the sister of T’.
R – T means ‘R is the father of T’.
R * T means ‘R is t...
Six movies M1, M2, M3, M4, M5 and M6 are released in months from Jamiary to June. M1 is not the first or last movie to be released. M3 is released just ...
Form a meaningful word with the third, fourth, fifth, seventh and tenth letters of the word 'PERSONALITY'. What is the last letter of that word?
If BARK is coded as 2-1-18-11, then what is the code for WEAP?
If the year is NOT a leap year, then how is the last day of the year and first day of the same year related?
In a certain code language,
A + B means ‘A is the wife of B’.
A − B means ‘A is the son of B’.
A × B means ‘A is the h...
Find the analogous pair.
Annex : Lessening
'L' is the son of 'K' whose only daughter is G. 'L' is the son of 'B', whose wife is A How is A related to G?
Identify the figure given in the options that when put in place of the question mark (?) will logically complete the series.