Polymorphism Example in Java

import java.io.*;
class polymorphism
{
	public static void main(String args[])throws IOException
	{
		BufferedReader obj = new BufferedReader (new InputStreamReader(System.in));
		System.out.println("Enter choice");
		System.out.println("1. Calculate area of circle");
		System.out.println("2. Calculate area of rectangle");
		int ch = Integer.parseInt(obj.readLine());
		switch(ch)
		{
		case 1:
			{
				System.out.println("Enter radius of circle");
				int r = Integer.parseInt(obj.readLine());
				System.out.println("Area of circle = "+area(r));
				break;
			}
		case 2:
			{
				System.out.println("Enter width of rectangle");
				int w= Integer.parseInt(obj.readLine());;
				System.out.println("Enter height of rectangle");
				int h=Integer.parseInt(obj.readLine());
				System.out.println("Area of rectangle = "+area(w,h));
			}
		}
	}
	public static double area(int r)
	{
		return 3.14*r*r;
	}
	public static int area(int w, int h)
	{
		return w*h;
	}
}

/* Output

Enter choice
1. Calculate area of circle
2. Calculate area of rectangle
1
Enter radius of circle
5
Area of circle = 78.5


Enter choice
1. Calculate area of circle
2. Calculate area of rectangle
2
Enter width of rectangle
5
Enter height of rectangle
10
Area of rectangle = 50

*/

Leave a Reply

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