In this tutorial, we will learn about the Polymorphism in Java or Varargs in Java. How to use them with the help of examples.

Polymorphism in Java

Polymorphism means is many forms. It occurs when we have many classes that are relate to each other by inheritance is know as Polymorphism.  In other words, One thing with many forms. Polymorphism is a one of the important features of Object-Oriented Programming. Polymorphism is derive from two Greek word :- poly and morphs. The word poly means ‘many’ and morphs means ‘forms’. A concept by which we can perform a single action in different ways.

Types

In Java polymorphism is divides into two types:-

  • Compile-time Polymorphism
  • Runtime Polymorphism

Compile-time Polymorphism

Compile-time is also know as static polymorphism. A Compile-time of polymorphism is achieve by function overloading or operator overloading. Java doesn’t support the Operator Overloading.

Example of Compile-time Polymorphism

Input Value

class abms{  
    void run(){System.out.println("hello");}  
  }  
  class key extends abms{  
    void run(){System.out.println("Student Basic engineer");}  
    
    public static void main(String args[]){  
      abms b = new key();
      b.run();  
    }  
  }  
Example of Compile-time Polymorphism

Let’s run the code

Input Value

Output Value

Output Value

Runtime Polymorphism

Runtime Polymorphism is also know as Dynamic polymorphism.

Varargs in Java

Varargs feature introduced in java 5.0 and later. This feature minimize the complexity of function overloading. This feature provided facility to define arguments using varargs that can accept multiple parameters value of same (mentioned) type. The Varargs uses a symbol called ellipsis (…) for differentiating data type and list variable or class and list object. Actually, it creates a list of array of parameters values.

Syntax Varargs in Java

return_type method_name(data_type... variableName){} 
Syntax Varargs in Java

Example of Varargs in Java

Input Value

class abms{ 
    static void display(String... values){  
        System.out.println("basic engineer ");  
       }  
        
       public static void main(String args[]){  
        
       display();   
       display("hello","student","is","varargs");  
       }   
      }  
Example of Varargs in Java

Let’s run the code

Input Value

Output Value

Output Value

If you have any queries regarding this article or if I have missed something on this topic, please feel free to add in the comment down below for the audience. See you guys in another article.

To know more about JAVA Wikipedia please click here .

Stay Connected Stay Safe, Thank you


Basic Engineer

Hey Readers! We have more than fifteen years of experience in Software Development, IoT, Telecom, Banking, Finance and Embedded domain. Currently we are actively working on Data Science, ML and AI with multiple market leaders worldwide. Happy Reading. Cheers!

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *