Inheritance Example 3- Room - Java

Program:
class Room
{
   int length,width;
   Room(int a,int b)
   {
     length = a;
     width = b;
   }
   void area()
   {
     int area = length*width;
     System.out.println("The area of the room is " +area);
   }
}

class roomvol extends Room
{
   int height;
   roomvol(int a,int b,int c)
   {
     super(a,b);
     height = c;
   }
   void volume()
   {
     int volume = length*width*height;
     System.out.println("The volume of the room is " +volume);
   }
}

class inheritance3
{
   public static void main(String args[])
   {
     roomvol room2 = new roomvol(10,40,20);
     room2.area();
     room2.volume();
   }
}

Output:
nn@linuxmint ~ $ javac inheritance3.java
nn@linuxmint ~ $ java inheritance3
The area of the room is 400
The volume of the room is 8000
nn@linuxmint ~ $

2 comments:

  1. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai | chennai's no.1 java training in chennai

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...