Tuesday, 29 August 2017

Java programming tutorial to demonstrate Constructor Overloading

Create a class Box and display the area of box using a method area(). Initialize all the variables using Constructor and demostrate Constructor Overloading.

Program

class Box{
          int w,l;
          Box(){
                   w=10;
                   l=10;
          }
          Box(int a,int b){
                   w=a;
                   l=b;
          }
          void area(){
                   System.out.println("Area of Box is  : "+(w*l));
          }
}
class Main{
          public static void main(String []args){
                   Box b1 = new Box();
                   Box b2 = new Box(50,60);
                  
                   b1.area();
                   b2.area();
          }

}

Output

Constructor Overloading java core

0 comments:

Post a Comment