-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerHierarchy.java
More file actions
76 lines (63 loc) · 1.93 KB
/
Copy pathPlayerHierarchy.java
File metadata and controls
76 lines (63 loc) · 1.93 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Base class: Player
class Player {
String name;
int age;
String position;
// Constructor
Player(String name, int age, String position) {
this.name = name;
this.age = age;
this.position = position;
}
// Common method for all players
void play() {
System.out.println(name + " is playing as a " + position);
}
void train() {
System.out.println(name + " is training.");
}
}
// Cricket Player subclass
class Cricket_Player extends Player {
Cricket_Player(String name, int age, String position) {
super(name, age, position);
}
@Override
void play() {
System.out.println(name + " is playing cricket as a " + position);
}
}
// Football Player subclass
class Football_Player extends Player {
Football_Player(String name, int age, String position) {
super(name, age, position);
}
@Override
void play() {
System.out.println(name + " is playing football as a " + position);
}
}
// Hockey Player subclass
class Hockey_Player extends Player {
Hockey_Player(String name, int age, String position) {
super(name, age, position);
}
@Override
void play() {
System.out.println(name + " is playing hockey as a " + position);
}
}
// Main class to test the hierarchy
public class PlayerHierarchy {
public static void main(String[] args) {
Cricket_Player cricketer = new Cricket_Player("Virat", 34, "Batsman");
Football_Player footballer = new Football_Player("Messi", 36, "Forward");
Hockey_Player hockeyPlayer = new Hockey_Player("Dhyan", 40, "Midfielder");
cricketer.play();
cricketer.train();
footballer.play();
footballer.train();
hockeyPlayer.play();
hockeyPlayer.train();
}
} // <-- Ensuring the main class is properly closed