/* program of constructor overloading in java */
class addition{
//constructor declaration two argument
addition(int a, int b){
// perform sum of two numbers
int sum = a+b;
System.out.println("sum of two numbers :"+sum);
}
//three argument constructor
addition(int a, int b, int c){
// perform sum of three numbers
int sum = a+b+c;
System.out.println("sum of three numbers :"+sum);
}
public static void main(String args[]){
addition obj = new addition(2,2); //called two argument constructor
addition obj1 = new addition(3,3,3); // called three argument constructor
}
}
class addition{
//constructor declaration two argument
addition(int a, int b){
// perform sum of two numbers
int sum = a+b;
System.out.println("sum of two numbers :"+sum);
}
//three argument constructor
addition(int a, int b, int c){
// perform sum of three numbers
int sum = a+b+c;
System.out.println("sum of three numbers :"+sum);
}
public static void main(String args[]){
addition obj = new addition(2,2); //called two argument constructor
addition obj1 = new addition(3,3,3); // called three argument constructor
}
}
No comments:
Post a Comment