0% found this document useful (0 votes)
10 views9 pages

Lec 10 POlymorphism

Polymorphism in object-oriented programming (OOP) refers to the ability of different classes related by inheritance to implement methods in various ways, allowing a single action to be performed differently. It provides benefits such as code reusability, flexibility, easier maintenance, and extensibility by enabling the addition of new classes without modifying existing code. An example is the base class 'Animal' with a method 'animalSound()', which can be overridden by derived classes like 'Pig' and 'Dog' to produce specific sounds.

Uploaded by

itzlegend156
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Lec 10 POlymorphism

Polymorphism in object-oriented programming (OOP) refers to the ability of different classes related by inheritance to implement methods in various ways, allowing a single action to be performed differently. It provides benefits such as code reusability, flexibility, easier maintenance, and extensibility by enabling the addition of new classes without modifying existing code. An example is the base class 'Animal' with a method 'animalSound()', which can be overridden by derived classes like 'Pig' and 'Dog' to produce specific sounds.

Uploaded by

itzlegend156
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Polymorphism in OOP

Polymorphism
• Polymorphism means "many forms", and it occurs when we have
many classes that are related to each other by inheritance.
• Inheritance lets us inherit attributes and methods from another class.
• Polymorphism uses those methods to perform different tasks.
• This allows us to perform a single action in different ways.
For example, think of a base class called Animal that has a method
called animalSound().
• Derived classes of Animals could be Pigs, Cats, Dogs, Birds-
And they also have their own implementation of an animal sound (the
pig oinks, and the cat meows, etc.)
Polymorphism and it’s Types
Now we can create Pig and Dog objects and override the animalSound() method:
Benefits of Polymorphism

Code Reusability:
Polymorphism allows us to reuse code by invoking methods via a
common interface, which reduces code duplication and improves
modularity.
Flexibility:
By considering objects of multiple classes as instances of a superclass,
we may build generic code that works with various objects, enhancing
flexibility and adaptability.
Maintenance:
Polymorphism improves cleaner code by decoupling object behavior
from specific implementations. This reduces code maintenance and
improves readability.
Extensibility:
Polymorphism allows for creating new classes without altering existing
code. New subclasses can be easily incorporated into the system,
allowing for easy extension.

You might also like