//program to demonstrate function overloading
// Function overloading in java
// Function overloading in java
Description
Function overloading refers to the use of two or more functions with the same name but with different parameters. The function to execute at run time is identified by the number and type of the parameters. This program defines two functions sumnop(), one without parameter and the other with two parameters. The function call sm.sumnop(); executes the function public void sumnop() and the function call sm.sumnop(x,y); executes the function public void sumnop(int p,int q).
Program
import java.io.*; class Sum { int a,b,c; public void sumnop() { this.a=10; this.b=5; this.c=a+b; System.out.println("Value of a is "+a+"\nValue of b is "+b); System.out.println("Sum is "+c); } public void sumnop(int p,int q) { c=p+q; System.out.println("Sum is "+c); } public static void main(String s[]) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int x,y; Sum sm=new Sum(); System.out.println("Function without parameter"); sm.sumnop(); System.out.println("Function with parameter"); System.out.println("Enter the value for a and b"); x=Integer.parseInt(br.readLine()); y=Integer.parseInt(br.readLine()); sm.sumnop(x,y); } }
Output
Function Overloading in Java |
No comments:
Post a Comment