Free Ebook cover Complete Logic Programming Course for Beginners

Complete Logic Programming Course for Beginners

4

(3)

83 pages

Encapsulation: Polymorphism

Capítulo 71

Estimated reading time: 3 minutes

Audio Icon

Listen in audio

0:00 / 0:00

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.

Continue in our app.

You can listen to the audiobook with the screen off, receive a free certificate for this course, and also have access to 5,000 other free online courses.

Or continue reading below...
Download App

Download the app

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.

Polymorphism in OOP allows an instance of a polymorphic type to be treated as an instance of multiple classes, usually parent and derived classes. This promotes code reuse, as the same interface can be used for different underlying forms. It enhances extensibility because new classes can be added with minimal changes. It also provides flexibility by allowing the same code structure to work with different data types.

Next chapter

Encapsulation: Error Handling

Arrow Right Icon
Download the app to earn free Certification and listen to the courses in the background, even with the screen off.