English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about polymorphism, different types of polymorphism, and how to implement them in Java through examples.
Polymorphism is an important concept in object-oriented programming. It simply means more than one form. That is, the behavior of the same entity (method, operator, or object) will be different in different situations. For example,
In Java+Operators are used to perform two specific functions. When used with numbers (integers and floating-point numbers), they perform addition operations.
int a = 5; int b = 6; int sum = a + b; // sum = 11
When we add+When an operator is used with a string, it will perform string concatenation. For example,
String firstName = "abc \"; String lastName = "xyz"; name = firstName + lastName; // name = abc xyz
In Java, polymorphism can be divided into two types:
Runtime polymorphism
Compile-time polymorphism
In Java, runtime polymorphism can be achieved through method overriding.
Assuming the same method is created in the superclass and its subclasses. In this case, the method to be called depends on the object used to call the method. For example,
abstract class Animal { public abstract void makeSound(); } class Dog extends Animal { @Override class Cat extends Animal { System.out.println("Bark bark.."); } } System.out.println("Bark bark.."); @Override class Cat extends Animal { public void makeSound() { } } class Main { public static void main(String[] args) { System.out.println("Meow meow..");1 = new Dog(); d1c Dog d1 Cat c = new Cat();1c } }
Output:
.makeSound(); Bark bark…-Meow
meow...To understand how method overriding works, please visit.
Method overriding in Java
In the above example, the method makeSound() has different implementations in two different classes. When we run the program1expression d1is an object of the Dog class. .makeSound() will call the method of the Dog class. This is because d
expression c1.makeSound() will call the method of the Cat class. This is because c1is an object of the Cat class.
Determines the method to be called during program execution. Therefore, method overriding is runtime polymorphism.
Compile-time polymorphism can be achieved through method overloading and operator overloading in Java.
In Java classes, if the parameters are different, you can create methods with the same name. For example,
void func() { ... } void func(int a) { ... } float func(double a) { ... } float func(int a, float b) { ... }
This is called method overloading in Java.
Let's take method overloading as an example.
class Demo { public void displayPattern() { for(int i = 0; i < 10; i++) { System.out.print("*"); } } public void displayPattern(char symbol) { for(int i = 0; i < 10; i++) { System.out.print(symbol); } } } class Main { public static void main(String[] args) { Demo d1 = new Demo(); d1.displayPattern(); System.out.println("\n"); d1.displayPattern('#'); } }
Output:
********** ##########
In the above program, the displayPattern() method has been overloaded.
If we call the method without passing any parameters, it will create * Pattern of characters.
If we call the method by passing a character as a parameter, it will create a pattern of # characters.
For more information about method overloading, please visitMethod overloading
In the case of method overriding, the method should be in different classes. However, in the case of method overloading, the method should be in the same class.
Method overriding is executed at runtime, while method overloading is executed at compile time.
Some operators in Java behave differently with different operands. For example,
+ Operators are overloaded to perform numeric addition and string concatenation.
&、| and ! are overloaded for logical and bitwise operations.
Let's see how operators are overloaded in Java.
In Java+Operators are used to perform two specific functions. When used with numbers (integers and floating-point numbers), they perform addition operations. For example,
int a = 5; int b = 6; int sum = a + b; // sum = 11
When we add+When an operator is used with a string, it will perform string concatenation. For example,
String firstName = "abc \"; String lastName = "xyz"; name = firstName + lastName; // name = abc xyz
In languages like C ++In such languages, we can define operators to perform different operations on different operands. However, Java does not support user-defined operator overloading.
Polymorphism allows us to create consistent code. For example,
Suppose we need to draw a circle and a square. To do this, we can create a Polygon class, with Circle and square as two subclasses. In this case, it is necessary to create a method with the same name render() in both subclasses instead of creating methods with different names.
In our method overloading example, we use the same method name displayPattern() to display two different patterns to maintain consistency.
The print() method in Java is also an example of polymorphism (method overloading). The same method is used to print values of different types, such as char, int, String, etc. We can also use the same method to print multiple values at once.
In Java, object variables (instance variables) represent the behavior of polymorphic variables. This is because the object variables of a class can refer to objects of its class and its subclasses. For example,
class Animal { public void displayInfo() {}} System.out.println("I am an animal."); } } class Dog extends Animal { @Override public void displayInfo() {}} System.out.println("I am a dog."); } } class Main { public static void main(String[] args) { //Declaration of Animal class object variable a1 Animal a1; //creation of Animal class object a1 = new Animal(); a1.displayInfo(); //creation of Dog class object a1 = new Dog(); a1.displayInfo(); } }
Output:
I am an animal. I am a dog.
In the above example, we created an object variable a of the Animal class1. Here, a1is a polymorphic variable.
This is because,
In the statement a1 = new Animal(), a1Reference to an object of the Animal class.
In the statement a1 = new Dog(), a1Reference to an object of the Dog class.
This is an example of upcasting in Java. For more information, please visitJava Upcasting and Downcasting.