Parameterized constructor using Java



Write a program in Java to define two classes, main and a sub
class.


v Create a method
within the subclass and call it from the main class


v Create a
parameterized constructor





Program:





class
Rectangle


 {


  float length,breadth;


  Rectangle(float a,float b)


   {


   length=a;


   breadth=b;


   }


  float area()


   {


    return (length*breadth);


   }


 }





 class Sk04


 {


  public static void main(String args[])


   {


    Rectangle ob1=new Rectangle(5.5f,6.5f);


    float ar1=ob1.area();


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


   }


 }





Compile:


D:\>javac
Sk04.java





Run:


D:\>java
Sk04





Output:


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