java extending interfaces

Solutions on MaxInterview for java extending interfaces by the best coders in the world

showing results for - "java extending interfaces"
Sophie
11 Jun 2017
1// An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface. 
2
3Example
4// Filename: Sports.java
5public interface Sports {
6   public void setHomeTeam(String name);
7   public void setVisitingTeam(String name);
8}
9
10// Filename: Football.java
11public interface Football extends Sports {
12   public void homeTeamScored(int points);
13   public void visitingTeamScored(int points);
14   public void endOfQuarter(int quarter);
15}
16
17// Filename: Hockey.java
18public interface Hockey extends Sports {
19   public void homeGoalScored();
20   public void visitingGoalScored();
21   public void endOfPeriod(int period);
22   public void overtimePeriod(int ot);
23}
24The Hockey interface has four methods, but it inherits two from Sports. Thus, a class that implements Hockey needs to implement all six methods. Similarly, a class that implements Football needs to define the three methods from Football and the two methods from Sports.