-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolymorphism5.java
58 lines (49 loc) · 1.62 KB
/
Polymorphism5.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
//EXPERIMENT 5
/* Write a java program to create an abstract class named Shape that contains an empty method named numberOfSides( ).
Provide three classes named Rectangle, Triangle and Hexagon such that each one of the classes extends the class Shape.
Each one of the classes contains only the method numberOfSides( ) that shows the number of sides in the given geometrical structures. (Exercise to understand polymorphis).*/
// Abstract class Shape
abstract class Shape {
// Abstract method numberOfSides, which must be implemented by subclasses
abstract void numberOfSides();
}
// Rectangle class extending Shape
class Rectangle extends Shape {
@Override
void numberOfSides() {
System.out.println("A rectangle has 4 sides.");
}
}
// Triangle class extending Shape
class Triangle extends Shape {
@Override
void numberOfSides() {
System.out.println("A triangle has 3 sides.");
}
}
// Hexagon class extending Shape
class Hexagon extends Shape {
@Override
void numberOfSides() {
System.out.println("A hexagon has 6 sides.");
}
}
// Main class to test the implementation
public class Shape5 {
public static void main(String[] args) {
// Create instances of Rectangle, Triangle, and Hexagon
Shape rectangle = new Rectangle();
Shape triangle = new Triangle();
Shape hexagon = new Hexagon();
// Call the numberOfSides method for each shape
rectangle.numberOfSides();
triangle.numberOfSides();
hexagon.numberOfSides();
}
}
//SAMPLE OUTPUT
/*
A rectangle has 4 sides.
A triangle has 3 sides.
A hexagon has 6 sides.
*/