Core Java - SPLessons

Polymorphism in Java

Chapter 22

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Polymorphism in Java

Polymorphism in Java

shape Description

Polymorphism in Java, polymorphism is the OOPS concept that means one can perform a single action by various ways, actually poly means many and morph means forms, these both words are Greek words. There are two types in polymorphism, one is compile time polymorphism and the second one is runtime polymorphism. By using method overloading and method overriding developer can perform polymorphism.


shape Conceptual
Figure


Compile Time Polymorphism

shape Description

Polymorphism in Java, In compile time polymorphism, a method definition gets bound to the method declaration at the time of compilation done by the compiler. This type of binding is known as static binding because once it is bound, rebinding is not possible. Compile time polymorphism is also known as static polymorphism. Overloading a static method is the best example for the compile time polymorphism. Following is an example for Polymorphism in Java.

public class staticmethodoverload {
static void splesson(){
	System.out.println("Hello Splessons");
}
static void splesson(int a,int b){
	System.out.println("This is second method");
	
}
public static void main(String args[]){
	staticmethodoverload.splesson();
	staticmethodoverload.splesson(12,0);
}
}

In this example, developer used two static methods then it is possible to overload static methods as follows.

static void splesson()
static void splesson(int a,int b)

Output
When compile the code following is the result will be displayed.

hai
second method

Run Time Polymorphism

shape Description

Polymorphism in Java, Method overriding concept is an example to understand the concept. Runtime polymorphism is also known as dynamic Polymorphism in Java. As discussed earlier overriding means it should have the same method with the same parameters, but the compiler does not know which method has to be called first, let’s follow the example.

public class X
{
    public void methodA() //Base class method
    {
        System.out.println ("Message of methodA()");
    }
}

public class Y extends X
{
    public void methodA() //Derived Class method
    {
        System.out.println ("Message of methodB()");
    }
}
public class Z
{
   public static void main (String args []) {
       X obj1 = new X(); // Reference and object X
       X obj2 = new Y(); // X reference but Y object
       obj1.methodA();
       obj2.methodA();
   }
}

Where methods are same, but compiler does not know which has to be executed so here object has been created as follows.

 X obj1 = new X(); 
       X obj2 = new Y(); 
       obj1.methodA();
       obj2.methodA();

Output
When compile the code result will be as follows.

Message of methodA()
Message of methodB()

Example of Java Runtime Polymorphism through upcasting

The following is an simple example to understand the overview of an upcasting.

class A{}  
class B extends A{}  
A a=new B();//upcasting  

In the above example, reference variable of Parent class(A) refers to the object of Child class(B) so this is known as upcasting. The following is an example by using polymorphism.

class Software{  
  void run(){System.out.println("running");}  
}  
class Facebook extends Software{  
  void run(){System.out.println("Having more than 100 million users");}  
  
  public static void main(String args[]){  
    Software b = new Facebook ();//upcasting  
    b.run();  
  }  
}  

In the above example also upcasting has been done successfully so when you compile the code result will be as follows.

Having more than 100 million users


Summary

shape Key Points

  • Polymorphism in Java, static method overloading is possible.
  • One has to mention two static methods to achieve static method overloading.