17.7. Classes and Objects: Polymorphism
Page 51 | Listen in audio
Polymorphism is one of the four fundamental pillars of Object Oriented Programming (OOP). The term comes from the Greek, where "poly" means many and "morphos" means forms. Therefore, polymorphism is the ability of an object to take many forms. In programming, this translates into the ability of a variable, function or object to represent more than one data type.
To understand polymorphism, it is helpful to first understand classes and objects. A class is a template or blueprint that describes the variables and methods common to all objects of a certain type. An object is an instance of a class. When a class is defined, no memory space is allocated. Only when an object of the class is created is memory allocated.
For example, consider a class called "Animal". This class can have variables like "name" and "age" and methods like "eat" and "sleep". Now, we can create objects of this class, like "dog" and "cat", which are both animals, but behave differently. The dog can bark, while the cat can meow. This is where polymorphism comes into play.
In polymorphism, a class can have many methods with the same name but different implementations. For example, the "Animal" class can have a "sound" method that produces different sounds depending on the type of animal. For the "dog" object, the "sound" method might return "bark", while for the "cat" object, it might return "meow".
There are two basic types of polymorphism: static (or overloading) and dynamic (or substitution).
Static Polymorphism occurs at compile time. Here, the method to be invoked is determined by the compiler. Method overloading and operator overloading are examples of static polymorphism.
On the other hand, Dynamic Polymorphism occurs at runtime. Here, the method to be invoked is determined at runtime. Method override is an example of dynamic polymorphism.
For example, suppose we have a superclass called "Animal" and two subclasses called "Dog" and "Cat". The superclass has a method called "sound" which is overridden by subclasses. If we create an object of the superclass and an object of each of the subclasses, and then call the "sound" method for each of them, the result will be different for each object, even though the method called is the same. This is polymorphism.
Polymorphism is a powerful tool that allows programmers to write cleaner and more understandable code, and is one of the fundamental concepts of Object Oriented Programming. It allows programmers to manipulate objects of different classes through a common interface, which can facilitate code maintenance and expansion.
In summary, polymorphism is a fundamental concept in object-oriented programming that allows a single interface to represent a generality of classes. It provides a means of integrating complexity by allowing the same code to be used to represent different types of behavior.
Now answer the exercise about the content:
What is Polymorphism in Object Oriented Programming (OOP)?
You are right! Congratulations, now go to the next page
You missed! Try again.
Next page of the Free Ebook: