Demonstrate Constructor Overloading and Method Overloading in JAVA

Write a JAVA program to demonstrate constructor overloading and method overloading?

Java Codes @ RabinsXP
Java Codes @ RabinsXP
Java Program

import java.io.*;
class sum
{
 int a,b;
 double c,d;
 String s1,s2;
 sum()
 {
  a=100;b=200;
 }
 sum(double x1,double y1)
 {
  c=x1;d=y1;
 }
 sum(String m, String n)
 {
  s1=m;
  s2=n;
 }
}
class overloading
{
 int a,b,is;
 double x,y,ds;
 String c1,c2,ss;
 int add(int a,int b)
 {
  is=a+b;
  return is;
 }
 double add(double x,double y)
 {
  ds=x+y;
  return ds;
 }
 
void add(String ch1,String ch2)
 {
  ss=ch1+ch2;
  System.out.println("Sum of 2 strings:"+ss);
  
 }
}
class Demo
{
 public static void main(String args[])
 {
  sum S1=new sum();
  System.out.println("Value of first constructor="+S1.a+", "+S1.b+"n");
  sum S2=new sum(1.3,2.7);
  System.out.println("Value of Second constructor="+S2.c+", "+S2.d+"n");
  sum S3=new sum("RABI","NS");
  System.out.println("Value of Third constructor="+S3.s1+" ,"+S3.s2+"n");
  overloading o1=new overloading();
  o1.a1=o1.add(S1.a,S1.b);
  o1.a2=o1.add(S2.c,S2.d);
  o1.add(S3.s1,S3.s2);
  System.out.println("Sum of 2 integers:"+o1.is);
  System.out.println("Sum of 2 floating numbers:"+o1.ds);
   
 }
}

Observed output:
Value of first constructor=100, 200

Value of first constructor=1.3, 2.7

Value of first constructor=RABI ,NS

Sum of 2 strings:RABINS
Sum of 2 integers:300
Sum of 2 floating numbers:3.0

Leave a Reply

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