The word Polymorphism is a combination of two Greek words, Poly meaning many and Morph meaning forms.
In programming, polymorphism refers to the same object exhibiting different forms and behaviors.
For example, take the Shape Class. The exact shape you choose can be anything. It can be a rectangle, a circle, a polygon, or a diamond. While, these are all shapes, their properties are different. This is called polymorphism.
xxxxxxxxxx
POLYMORPHISM: It is an ability of object to behave in multiple
form. The most common use of polymorphism is Java, when a
parent class reference type of variable is used to refer to a child
class object.
In my framework
E.g.: WebDriver driver = new ChromeDriver();
We use method overloading and overriding to achieve
Polymorphism.
There are two types of achieving Polymorphism
Upcasting & Downcasting
Upcasting is casting a subtype to a supertype,
going up in the inheritance tree.
It is done implicitly
in order to use method available on any
interface/class, the object should be of
same class or of class implementing the interface.
WebDriver driver = new ChromeDriver();
or
TakeScreenShot ts = new ChromeDriver();
Downcasting is casting to a subtype,
going down in the inheritance tree.
It is done to access sub class features.
It has to be done manually
ChromeDriver driver = (ChromeDriver)Webdriver;
xxxxxxxxxx
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Real life example of polymorphism: A person at the same time can have different characteristic. Like a man at the same time is a father, a husband, an employee.
xxxxxxxxxx
// Parent class
class Animal {
public void greeting() {
System.out.println("The animal greets you.");
}
}
// Child class
class Cat extends Animal {
public void greeting() {
System.out.println("The cat meows.");
}
}
class MainClass {
public static void main(String[] args) {
Animal animal1 = new Animal(); // Animal object
Animal cat1 = new Cat(); // Cat object
animal1.greeting(); // prints "The animal greets you."
cat1.greeting(); // prints "The cat meows."
}
}
xxxxxxxxxx
// Java Program for Method Overriding
// Class 1
// Helper class
class Parent {
// Method of parent class
void Print()
{
// Print statement
System.out.println("parent class");
}
}
// Class 2
// Helper class
class subclass1 extends Parent {
// Method
void Print() { System.out.println("subclass1"); }
}
// Class 3
// Helper class
class subclass2 extends Parent {
// Method
void Print()
{
// Print statement
System.out.println("subclass2");
}
}
// Class 4
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of class 1
Parent a;
// Now we will be calling print methods
// inside main() method
a = new subclass1();
a.Print();
a = new subclass2();
a.Print();
}
}
What is Polymorphism:
xxxxxxxxxx
1] Polymorphism is one of the important features of Object-Oriented Programming.
2] Polymorphism has ability to perform more than one form
3] Ploymorphism allows us to perform a single function in different ways
xxxxxxxxxx
class Animal:
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!"
class Duck(Animal):
def speak(self):
return "Quack!"
def animal_sound(animal):
return animal.speak()
# Creating instances of different classes
dog = Dog()
cat = Cat()
duck = Duck()
# Using the common base class and polymorphism
animals = [dog, cat, duck]
for animal in animals:
print(f"{animal.__class__.__name__} says: {animal_sound(animal)}")