Method Overloading in Java or program to demonstrate method Overloading concept. rcub java

 

Method Overloading in Java

A class has multiple methods having same name but different in signatures
where the signature can differ by changing number of arguments or  changing the data type or both.

Overloading is related to compile-time (or static) polymorphism.


Example:
program to demonstrate method Overloading in Java.

public class Sum
{
int add(int x, int y)
{
return x+y;
}
int add(int x, int y, int z)
{
return x+y+z;
}
double add(double x, double y)
{
return x+y;
}
public static void main(String args[])
{
  Sum s=new Sum();
  System.out.println ("addition of 2 integers ="+s.add(10,20));
System.out.println ("addition of 3 integers ="+s.add(10,20,20));
System.out.println ("addition of 2 floating point="+s.add(10.5,20.5));
}
}


Output:
addition of 2 integers =30
addition of 3 integers =50
addition of 2 floating point=31.0

Comments

Popular posts from this blog

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.