Java program that illustrates how run time polymorphism is achieved

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class figure
{
	double d1,d2;
	figure(double a,double b)
	{
		d1=a;
		d2=b;
	}
	double area()
	{
		System.out.println("Area of the figure");
		return 0;
	}
}
class rectangle extends figure
{
	rectangle(double a,double b)
	{
		super(a,b);
	}
	double area()
	{
		System.out.println("Area of rectangle");
		return d1*d2;
	}
}
class triangle extends figure
{
	triangle(double a,double b)
	{
		super(a,b);	
	}
	double area()
	{
		System.out.println("Area of triangle");
		return d1*d2/2;
	}
}
class runpoly
{
	public static void main(String[] args)
	{
		figure f=new figure(45,6);
		rectangle r=new rectangle(10,30);
		triangle t=new triangle(10,20);
		figure a;
		a=f;
		System.out.println(a.area());
		a=r;
		System.out.println(a.area());
		a=t;
		System.out.println(a.area());
	}
}

Output:
Area of figure
0.0
Area of rectangle
300.0
Area of triangle
100.0

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

Leave a Reply

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

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in