Constructor Overloading



Write a program in Java to implement Constructor Overloading.





Program:





class Box


 {


  float length,breadth;


  Box(float a,float b)


   {


   length=a;


   breadth=b;


   }





  Box(float a)


   {


   length=a;


   breadth=a;


   }


  float area()


   {


    return (length*breadth);


   }


 }





 class Sk05


 {


  public static void main(String args[])


   {


    Box ob1=new Box(5.5f);


    float ar1=ob1.area();


    System.out.println("Area =
"+ar1);


    Box ob2=new Box(5.5f,6.5f);


    float ar2=ob2.area();


    System.out.println("Area =
"+ar2);


   }


 }





Compile:


D:\>javac
Sk05.java





Run:


D:\>java
Sk05





Output:


Area =
30.05


Area =
35.75







Comments

Popular posts from this blog

Write a Program to Add two 3x3 Matrix using C

C program for Unit Conversion

Write a Program to Add two 5x5 Matrix using C