Article image Encapsulation: Polymorphism

19.10. Encapsulation: Polymorphism

Page 71 | Listen in audio

19.10. Encapsulation: Polymorphism

Polymorphism is one of the four fundamental pillars of object-oriented programming (OOP), along with abstraction, inheritance, and encapsulation. The term polymorphism comes from the Greek and means "many forms". In programming, it refers to the ability of an object to be treated as an instance of a parent class, thus allowing a single variable to represent multiple types of data.

Concept of Polymorphism

Polymorphism allows classes derived from a single base class to share a common design. This means that a member of a parent class can refer to a member of a child class. For example, an object of class 'Animal' can refer to an object of class 'Dog' or 'Cat'. This is possible because 'Dog' and 'Cat' are subclasses of 'Animal'.

Types of Polymorphism

There are two main types of polymorphism: static (or overload) and dynamic (or override).

Static Polymorphism

Static polymorphism occurs at compile time. It is achieved by overloading methods or operators. Method overloading means having two or more methods with the same name but different parameters. For example, you might have an 'Add' method that accepts two integers and another 'Add' method that accepts two floating point numbers.

Dynamic Polymorphism

Dynamic polymorphism occurs at runtime. It is achieved by substituting methods. Overriding methods means changing the functionality of a method in a derived class that is already defined in the parent class. For example, you might have a 'Speak' method in the 'Animal' class that is overridden in the 'Dog' class to make a barking sound and in the 'Cat' class to make a meowing sound.

Benefits of Polymorphism

Polymorphism is a powerful tool that brings flexibility and simplicity to code. Here are some of the benefits:

  • Code Reuse: With polymorphism, you can write code that works with objects of different classes. This means you can reuse the same code for different types of objects.
  • Extensibility: Polymorphism allows you to add new classes without changing existing code. This makes the system more extensible and easier to maintain.
  • Flexibility: Polymorphism allows you to write more flexible code that can handle different situations. For example, you can use a variable of type 'Animal' to refer to a 'Dog' or 'Cat' object depending on the situation.

Conclusion

In summary, polymorphism is a fundamental concept in object-oriented programming that allows an object to be treated as an instance of multiple classes. It offers code flexibility and extensibility, allowing you to reuse code and add new classes without changing existing code. Understanding polymorphism is essential for any programmer who wants to master object-oriented programming.

Now answer the exercise about the content:

What is polymorphism in object-oriented programming (OOP) and what are its main benefits?

You are right! Congratulations, now go to the next page

You missed! Try again.

Article image Encapsulation: Error Handling

Next page of the Free Ebook:

72Encapsulation: Error Handling

3 minutes

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text