What is pure polymorphism?
Solution
Pure polymorphism, also known as interface polymorphism, is a concept in object-oriented programming where methods can be implemented in multiple ways, but can be used interchangeably, regardless of where they were declared or how they were implemented.
Here's a step-by-step explanation:
-
Polymorphism is a fundamental concept in Object-Oriented Programming (OOP). It allows objects of different types to be treated as objects of a common, more general type.
-
The term "pure polymorphism" refers to the use of interfaces or abstract classes in programming languages like Java or C#. An interface or an abstract class defines a contract of methods that the implementing or subclassing concrete classes must provide.
-
When a method is called on an object of an interface or abstract class type, the appropriate method in the actual object's class is invoked at runtime. This is known as dynamic method dispatch or late binding.
-
This allows for flexibility and extensibility in your code. You can add new classes that implement the interface or inherit from the abstract class, and they can be used interchangeably with the existing classes.
-
For example, you might have an interface
Shapewith a methoddraw(). You can have classesCircle,Square,Triangle, etc., each implementingShapeand providing their own implementation ofdraw(). You can then have a list ofShapeobjects and calldraw()on each, and the correct method will be invoked depending on the actual type of the object. -
This is "pure" polymorphism because it's based purely on the interface or abstract class type, not on the actual class of the object. The code that uses the objects doesn't need to know or care about their actual classes, as long as they adhere to the contract defined by the interface or abstract class.
Similar Questions
. Explain the term polymorphism as applied in OOPS
Polymorphism is when a subclass can modify the behavior of its superclass.
untime polymorphism is also known as
How many types of polymorphism are there?
What is polymorphism in C++?The ability of a function or an object to take multiple forms.The ability to execute multiple functions simultaneously.The ability to inherit multiple classes.The ability to overloading operators.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.