I’m pretty much a junior level programmer and haven’t yet came across the requirement of using Polymorphism in my experiences so far.
My basic understanding of polymorphism is creating something that requires the ability to handle various different types objects.
I’m just wondering if anyone out there can give some decent examples of when & why you would encounter this.
Sorry if my question appears dumb, I’m only a learner.
Polymorphism is useful any time you have a common contract but behavior varies from object to object. For example, take the ubiquitous “Job”. You may have an interface like:
and then implement various jobs that adhere to this contract, such as a DataPollingJob, a MaintenanceJob, EmailJob, BalanceJob, etc. whatever is running the jobs doesn’t have to know what the implementation does or how it does it, but knows that it can call
Executewith a context to have that job perform its function.another common scenario in which polymorphism is helpful is in messaging. you may have many different types of messages, but they will all adhere to some basic contract that the messaging infrastructure understands.