-
Notifications
You must be signed in to change notification settings - Fork 0
/
AreaFinder.java
64 lines (52 loc) · 1.41 KB
/
AreaFinder.java
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
55
56
57
58
59
60
61
62
63
// Shape abstract class
abstract class Shape
{
// abstract method. Remember: abstract method has no body.
public abstract double area();
}
// Circle class inherited from shape class
class Circle extends Shape
{
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
// See this annotation @Override, it is telling that this method is from parent
// class Shape and is overridden here
@Override
public double area()
{
return 3.14 * radius * radius;
}
}
// Rectangle class inherited from shape class
class Rectangle extends Shape
{
private double length;
private double width;
Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
// See this annotation @Override, it is telling that this method is from parent
// class Shape and is overridden here
@Override
public double area()
{
return length * width;
}
}
public class AreaFinder
{
public static void main(String[] args)
{
//This will create an object of circle class
Shape circle = new Circle(5.0);
//This will create an object of Rectangle class
Shape rectangle = new Rectangle(5.4, 5.2);
System.out.println("Shape of circle : " + circle.area());
System.out.println("Shape of rectangle: " + rectangle.area());
}
}