-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolymorphismExample.java
More file actions
44 lines (39 loc) · 990 Bytes
/
PolymorphismExample.java
File metadata and controls
44 lines (39 loc) · 990 Bytes
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
// Shape class (parent class)
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}
// Circle class (subclass of Shape)
class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
// Square class (subclass of Shape)
class Square extends Shape {
@Override
public void draw() {
System.out.println("Drawing a square");
}
}
// Triangle class (subclass of Shape)
class Triangle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a triangle");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
// Creating objects of different shapes
Shape shape1 = new Circle();
Shape shape2 = new Square();
Shape shape3 = new Triangle();
// Calling the draw method on each shape
shape1.draw();
shape2.draw();
shape3.draw();
}
}