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
Example of Compile-time Polymorphismclass 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(); } }
Let’s run the code
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
Syntax Varargs in Javareturn_type method_name(data_type... variableName){}
Example of Varargs in Java
Input Value
Example of Varargs in Javaclass abms{ static void display(String... values){ System.out.println("basic engineer "); } public static void main(String args[]){ display(); display("hello","student","is","varargs"); } }
Let’s run the code
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
0 Comments