Inheritance Example in Java

import java.io.*;
class area
{
	int l,b;
	area(int i,int j)
	{
		l=i;
		b=j;
	}
	
	public int cal_area()
	{
		return l*b;
	}
}

class volume extends area
{
	int h;
	volume(int i, int j,int k)
	{
		super(i,j);
		h=k;
	}
	
	public int cal_vol()
	{
		return l*b*h;
	}
}

class in_main
{
	public static void main(String args[])throws IOException
	{
		BufferedReader obj = new BufferedReader (new InputStreamReader(System.in));
		System.out.println("Enter length of object");
		int l = Integer.parseInt(obj.readLine());
		System.out.println("Enter breath of object");
		int b = Integer.parseInt(obj.readLine());
		System.out.println("Enter height of object");
		int h= Integer.parseInt(obj.readLine());
		volume v = new volume(l,b,h);
		System.out.println("Area = "+v.cal_area());
		System.out.println("Volume = "+v.cal_vol());
	}
}

/* Output

Enter length of object
5
Enter breath of object
10
Enter height of object
7
Area = 50
Volume = 350

*/

Leave a Reply

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