Home / JAVA / Java program to implement polymorphism

Java program to implement polymorphism

Java program to implement polymorphism: Polymorphism is a technique in java with which you can create multiple functions with same name but each having different arguments.

It means that we have a program in which we have several functions with same name but have different arguments which are easy to use as if we put different names to the functions than it is difficult to remember which function we want to call.

So for easy maintenance and use we use this technique and call functions with same name and pass different parameters accordingly.

This method is also called overloading.

 

public class Polymorphism      /* class name is Polymorphism */
{
int a,b,sum; /* These are instance variables which are already initialized by default                      
             values. These can be directly used in instance functions. */

void get() /* This is instance functions which is used in main by making an object of                 
that function. Also this function name is used several times with different arguments to be used according to the requirements.
When this function is call from the main than we pass no arguments during the function call and values are automatically assigned to the addition function to print the sum. */ 

{
a=10;     // values are assigned to both the variables a and b.
b=20;
}
void get(int x,int y)  /* When this function is used than we pass 2 parameters in the function call */
{
a=x;     // value of x is assigned to a.
b=y;     // value of y is assigned to b.
}
void get(int x) /* When this function is used than we pass only parameter value during the function call */
{
a=x; // value of one parameter x is assigned to a.
b=x; // value of one parameter x is assigned to b.
}
void addition() /* This function is used with different get function call for performing the addition operation differently according to different parameters value. */
{
sum=a+b; // addition operation.
}
void display() /* This function is used to display the value of result of addition function call */
{
System.out.println("Addition of the variables as input is provided is "+ sum);
System.out.println(" ");
}
public static void main(String args[]) // main function for calling the functions.
{
Polymorphism obj=new Polymorphism(); /* create the object ‘obj’ of class which is used for calling the instance functions of class */ 
obj.get(); /* calling get function  without parameter values which is the 1st function being defined in this program */
obj.addition(); //calling the addition function.
obj.display(); // calling the display function.

obj.get(5); /* calling get function  with one parameter values which is the 3rd function being defined in this program */
obj.addition();//calling the addition function with different get function.
obj.display();//calling the display function.

obj.get(10,40); /* calling get function  with 2 parameter values which is the 2nd function being defined in this program */
obj.addition();//calling the addition function again with different get function.
obj.display();//calling the display function.

} // closing of main function
} // closing of class

 OUTPUT:

polymorphism

 

Tags: java program for polymorphism, java program to implement polymorpshim

Check Also

Java Program to find Mobius Function of number

Mobius Function Java Program:  The MOBIUS function µ(n) for a natural number N is defined as ...

Leave a Reply

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

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.