-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAreaFinder.java
More file actions
55 lines (44 loc) · 1.43 KB
/
AreaFinder.java
File metadata and controls
55 lines (44 loc) · 1.43 KB
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
package src;
// src.Shape abstract class
abstract class Shape {
// abstract method. Remember: abstract method has nobody.
public abstract double area();
}
// src.Circle class inherited from shape class
class Circle extends Shape {
private final double radius;
public Circle(double radius) {
this.radius = radius;
}
// See this annotation @Override, it is telling that this method is from parent
// class src.Shape and is overridden here
@Override
public double area() {
return 3.14 * radius * radius;
}
}
// src.Rectangle class inherited from shape class
class Rectangle extends Shape {
private final double length;
private final 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 src.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 src.Rectangle class
Shape rectangle = new Rectangle(5.4, 5.2);
System.out.println("src.Shape of circle : " + circle.area());
System.out.println("src.Shape of rectangle: " + rectangle.area());
}
}