0% found this document useful (1 vote)
331 views

Interface Exercise

1. The Hovercraft class cannot use multiple inheritance in Java, so interfaces are used instead. Interfaces allow a class to implement multiple interfaces, solving the issue of multiple inheritance. 2. Vehicle, LandVehicle, and SeaVessel classes are replaced with interfaces to be implemented by classes like Hovercraft, solving the maxSpeed property issue between inheritance hierarchies. 3. Interfaces cannot have properties, so getter and setter methods replace properties in the implemented classes.

Uploaded by

amna6shaur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
331 views

Interface Exercise

1. The Hovercraft class cannot use multiple inheritance in Java, so interfaces are used instead. Interfaces allow a class to implement multiple interfaces, solving the issue of multiple inheritance. 2. Vehicle, LandVehicle, and SeaVessel classes are replaced with interfaces to be implemented by classes like Hovercraft, solving the maxSpeed property issue between inheritance hierarchies. 3. Interfaces cannot have properties, so getter and setter methods replace properties in the implemented classes.

Uploaded by

amna6shaur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Interface Exercise

The Hovercraft class shown in the diagram inherits from both LandVehicle
and SeaVessel since the hover-craft is in the rather unique position of being
able to travel on land and sea. The Hovercraft class cannot be expressed in
Java since Java does not have the facility for multiple inheritance in classes.
All other classes in the diagram use only single inheritance and so they can
be expressed in Java.

One of the problems with multiple inheritance is in deciding what to do with


properties in a class like Vehicle that is an indirect superclass of Hovercraft
in two different ways, via LandVehicle and via SeaVessel. The hovercraft is
being able to drive on land and sea might have two different maximum
speeds, one for land travel and the other for sea travel. This leads to a
problem of what should be the appropriate value for the maxSpeed property
of Hovercraft objects? We could set maxSpeed to be the maximum of the two
speed values but then this might badly affect the behaviour of the drive
method which, because it is defined in the LandVehicle class, might assume
that the value of maxSpeed is the maximum speed attainable on land. A
similar problem arises with the launch method.
The solution of the above problem is to implement Interface instead of
classes. The most important feature of interfaces is that a class can implement
more than one interface. Interfaces are limited in two respects. Firstly, they
are not allowed to have any properties except static constants, and secondly
the methods of an interface must be defined without bodies, like abstract
methods. These two limitations prevent interfaces from suffering from the
problem that occurred with the maxSpeed property in the previous UML
diagram.
We can re-work the previous UML diagram into something that can be
expressed within the Java language by replacing the classes Vehicle,
LandVehicle and SeaVessel with interfaces IsVehicle, IsLandVehicle and
IsSeaVessel, respectively. The dotted arrows in the next UML diagram
indicate classes implementing interfaces.
Note that the Hovercraft class implements both the IsLandVehicle and
IsSeaVessel interfaces, rather than inheriting from two classes which is not
allowed in Java.
Since an interface is not allowed to have any properties except static
constants, we have replaced the properties that existed in the classes Vehicle,
LandVehicle and SeaVessel with “getter" and “setter" methods. That is to say
that, for each property X, there is now a pair of methods getX and setX. A
getX, setX pair of public methods in a class is logically equivalent for users
of the class to a public property called X.
Since the methods of the interfaces are defined without bodies, they are
defined in the classes Jeep, Hovercraft and Frigate that implement the three
interfaces.

Questions:
1. Write an interface IsEmergency which extends no other interface and
contains just one method soundSiren which takes no arguments and
returns no value.
2. Write a class PoliceCar that implements the IsEmergency and
IsLandVehicle interfaces.
3. In addition to the methods you have written for the PoliceCar class,
think of a new method or property that police cars have and add it to
the class.

You might also like