-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShapePoly.java
72 lines (62 loc) · 1.6 KB
/
ShapePoly.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
64
65
66
67
68
69
70
71
72
package sajal;
class Shape {
// Method to draw a shape
public void draw() {
System.out.println("Drawing a shape");
}
// Method to erase a shape
public void erase() {
System.out.println("Erasing a shape");
}
}
class Circle extends Shape {
// Override draw method for Circle
@Override
public void draw() {
System.out.println("Drawing a circle");
}
// Override erase method for Circle
@Override
public void erase() {
System.out.println("Erasing a circle");
}
}
class Triangle extends Shape {
// Override draw method for Triangle
@Override
public void draw() {
System.out.println("Drawing a triangle");
}
// Override erase method for Triangle
@Override
public void erase() {
System.out.println("Erasing a triangle");
}
}
class Square extends Shape {
// Override draw method for Square
@Override
public void draw() {
System.out.println("Drawing a square");
}
// Override erase method for Square
@Override
public void erase() {
System.out.println("Erasing a square");
}
}
public class ShapePoly {
public static void main(String[] args) {
// Creating an array of Shape objects
Shape[] shapes = new Shape[3];
shapes[0] = new Circle();
shapes[1] = new Triangle();
shapes[2] = new Square();
// Loop through the array and call draw and erase methods for each shape
for (Shape shape : shapes) {
shape.draw();
shape.erase();
System.out.println();
}
}
}