JAVA Program to Find Area of Room using Final Variable

JAVA Program to Find Area of Room using Final Variable:


// program to use the final variable

class Room
 {
  final int length=10;
  int breadth;

  Room(int y)   //constructor mthod
   {
   //length=x;
   breadth=y;
   }

   int area()
   {
    return(length * breadth);
   }
  }

  class  BedRoom extends Room
  {
   int height;
   BedRoom(int y,int z)
   {
   super(y);
   height=z;
   }

   int volume()
   {
    breadth=breadth+10;
    //length=length+10;
    // If you use final variable for changing the value of final
    // variable,you get error like Can't assign a value to the final variable : length
    System.out.println("Length  = "+length);
    System.out.println("Breadth = "+breadth);
    System.out.println("Height  = "+height);
    return( length*breadth*height);
   }
  }

  class finaliser
  {
   public static void main(String args[])
   {
   BedRoom b = new BedRoom(10,30);
   int a = b.area();
   int v = b.volume();
   System.out.println("Area = "+a);
   System.out.println("Volume = "+v);
   }
  }

OUTPUT:

Length  = 10
Breadth = 20
Height  = 30
Area = 100
Volume = 6000

Leave a Reply

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

twelve − 7 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.