Java Reference
In-Depth Information
public interface HockeyListener extends SportsListener
{
public void homeGoalScored();
public void visitingGoalScored();
public void endOfPeriod(int period);
public void overtimePeriod(int ot);
}
The HockeyListener interface has four methods, but it inherits two from
SportsListener; thus, a class that implements HockeyListener needs to imple-
ment all six methods. Similarly, a class that implements FootballListener needs
to define the three methods from FootballListener and the two methods from
SportsListener.
The following SportsTicker class implements the HockeyListener interface.
Notice that the required six methods are defined to avoid declaring Sports-
Ticker abstract.
public class SportsTicker implements HockeyListener
{
private String home, visitors;
private int homeGoals, visitorGoals;
private int period, overtimePeriod;
public void homeGoalScored()
{
homeGoals++;
}
public void visitingGoalScored()
{
visitorGoals++;
}
public void endOfPeriod(int period)
{
this.period = ++period;
}
public void overtimePeriod(int ot)
{
overtimePeriod = ot;
}
public void setHomeTeam(String name)
{
home = name;
}
public void setVisitingTeam(String name)
{
visitors = name;
}
}
Search WWH ::




Custom Search