87.5k views
3 votes
Polymorphism allows you to give the same name to two different methods if the methods are in different classes which are related through inheritance.

a) True
b) False
c) Partially true
d) Irrelevant

User BARNZ
by
7.3k points

1 Answer

7 votes

Answer: The statement that polymorphism allows you to give the same name to two different methods in unrelated classes related through inheritance is false.

Step-by-step explanation:

Polymorphism in object-oriented programming allows you to give the same name to methods that belong to different classes but have a common base class or interface. This concept is known as method overriding, where a subclass provides its own implementation of a method that is already defined in its superclass.

However, it is important to note that polymorphism does not allow you to give the same name to two different methods if they are in unrelated classes, even if those classes are related through inheritance.

For example, let's say we have two classes: Class A and Class B. Class B is a subclass of Class A, and both classes have a method called "doSomething". In this case, polymorphism allows us to override the "doSomething" method in Class B to provide a different implementation than the one in Class A. This means that when we call the "doSomething" method on an object of Class B, the overridden method in Class B will be executed.

However, if we have two unrelated classes, such as Class C and Class D, polymorphism does not allow us to give the same name to a method in Class C and a method in Class D. Each class can have its own method with the same name, but they are not related through inheritance and do not exhibit polymorphic behavior.

User Gregtzar
by
7.8k points