-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJava Method Overriding.java
55 lines (47 loc) · 1.55 KB
/
Java Method Overriding.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
import java.util.*;
// Parent Sports class
class Sports{
// Method to return the name of the sport
String getName(){
return "Generic Sports";
}
// Method to print the number of players in a team for the sport
void getNumberOfTeamMembers(){
System.out.println("Each team has n players in " + getName());
}
}
// Child Soccer class, inherits from Sports
class Soccer extends Sports{
// Override the getName() method of the parent class
@Override
String getName(){
return "Soccer Class";
}
// Override the getNumberOfTeamMembers() method of the parent class
void getNumberOfTeamMembers(){
System.out.println("Each team has 11 players in " + getName());
}
}
// Main class
public class Solution{
public static void main(String []args){
// Create an instance of the parent class
Sports c1 = new Sports();
// Create an instance of the child class
Soccer c2 = new Soccer();
// Print the name of the sport for the parent instance
System.out.println(c1.getName());
// Print the number of players in a team for the parent instance
c1.getNumberOfTeamMembers();
// Print the name of the sport for the child instance
System.out.println(c2.getName());
// Print the number of players in a team for the child instance
c2.getNumberOfTeamMembers();
}
}
// Output:
// Generic Sports
// Each team has n players in Generic Sports
// Soccer Class
// Each team has 11 players in Soccer Class
// Have a sweet day.