Polymorphism Flashcards
what is polymorphism?
The word polymorphism means having many forms. In simple words, we can define Java Polymorphism as the ability of a message to be displayed in more than one form.
What is Polymorphism in Java?
Polymorphism is considered one of the important features of Object-Oriented Programming. Polymorphism allows us to perform a single action in different ways. In other words, polymorphism allows you to define one interface and have multiple implementations. The word “poly” means many and “morphs” means forms, So it means many forms.
Types of Java Polymorphism
Compile-time Polymorphism
Runtime Polymorphism
Compile-time polymorphism
Compile-Time Polymorphism in Java
It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading.
Note: But Java doesn’t support the Operator Overloading.
Java Polymorphism
Method Overloading
When there are multiple functions with the same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by changes in the number of arguments or/and a change in the type of arguments.
Example 1:
// Java Program for Method overloading
// By using Different Types of Arguments
// Class 1
// Helper class
class Helper {
// Method with 2 integer parameters static int Multiply(int a, int b) { // Returns product of integer numbers return a * b; } // Method 2 // With same name but with 2 double parameters static double Multiply(double a, double b) { // Returns product of double numbers return a * b; } }
// Class 2
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Calling method by passing
// input as in arguments
System.out.println(Helper.Multiply(2, 4));
System.out.println(Helper.Multiply(5.5, 6.3));
}
}
Output
8
34.65
Runtime Polymorphism in Java
Runtime Polymorphism in Java
It is also known as Dynamic Method Dispatch. It is a process in which a function call to the overridden method is resolved at Runtime. This type of polymorphism is achieved by Method Overriding. Method overriding, on the other hand, occurs when a derived class has a definition for one of the member functions of the base class. That base function is said to be overridden.
Example
// 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(); } } Output subclass1 subclass2
Java - Overriding
In the previous chapter, we talked about superclasses and subclasses. If a class inherits a method from its superclass, then there is a chance to override the method provided that it is not marked final.
The benefit of overriding is: ability to define a behavior that’s specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.
In object-oriented terms, overriding means to override the functionality of an existing method.
Example
Let us look at an example.
class Animal {
public void move() {
System.out.println(“Animals can move”);
}
}
class Dog extends Animal {
public void move() {
System.out.println(“Dogs can walk and run”);
}
}
public class TestDog {
public static void main(String args[]) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move(); // runs the method in Animal class b.move(); // runs the method in Dog class } } Output Animals can move Dogs can walk and run In the above example, you can see that even though b is a type of Animal it runs the move method in the Dog class. The reason for this is: In compile time, the check is made on the reference type. However, in the runtime, JVM figures out the object type and would run the method that belongs to that particular object.
Therefore, in the above example, the program will compile properly since Animal class has the method move. Then, at the runtime, it runs the method specific for that object.
Consider the following example −