Write a Java Program to define a class, define instance methods and overload them and use them for dynamic method invocation.
Program Code:
import java.lang.*;
class add
{
void display(int a,int b)
{
int c=a+b;
System.out.println("The sum of " + a + " & " + b + " is " + c);
}
void display(double a,double b)
{ double c=a+b;
System.out.println("The sum of " + a + " & " + b + " is " + c);
}
}
class add_demo
{
public static void main(String arg[])
{
add obj=new add();
obj.display(10,20);
obj.display(10.2,20.2);
}
}
Output:
The sum of 10 & 20 is 30
The sum of 10.2 & 20.2 is 30.4
Post A Comment:
0 comments: