10-1 An Interface is a special kind of class which defines a specification of a set of methods. provides a guarantee that any class that implements this interface will provide these methods. is similar to setting "standards" and any class implementing it will get a "stamp of approval". A contract!!!!
10-2 Inheritance vs. interfaces Is different from inheritance since interfaces do not inherit state or behaviour, instead they just define a specification!!!!!!
10-3 Syntax interface interfaceName { /* Method specifications */ } class className implements interfaceName { /* Bodies for the interface methods */ /* Own data and methods. */ }
10-4 Example: MovableObject interface MovableObject { boolean start(); void stop(); boolean turn(int degrees); double fuelRemaining(); boolean changeSpeed(double kmPerHour); }
10-5 aPlane class Plane implements MovableObject { boolean start() { //Do what's necessary to start the plane. Return true if it actually started. } void stop() { //Do whatever is necessary to stop the plane. } boolean turn(int degrees) { // Do what's necessary to turn the plane. Return true if it worked. } double fuelRemaining() { //Return the amount of plane fuel remaining. } boolean changeSpeed(double kmPerHour) { //Do what's necessary to accelerate or decelerate by the given amount }
10-6 How does having an interface help us? It helps us extract common behaviour among similar objects (kind of like inheritance). It makes a system more robust and easier to understand in that it ensures that the similar objects have at least certain behavours.
10-7 A handheld remote control for MovableObjects. class RemoteControl { private MovableObject machine; // The constructor RemoteControl(MovableObject m) { machine = m; }... //When the start button is pressed on the remote Boolean okay = machine.start(); if (!okay) display("No Response on start");... } Works for Plane, Car, Train, Ship, Lawnmower,....
10-8 Using it!!! Can create remote controls for more than one class Each will operate correctly using its own implementation of the MovableObject interface Plane aPlane = new Plane(); Ship aShip = new Ship(); RemoteControl planeRemote = new RemoteControl(plane); RemoteControl shipRemote = new RemoteControl(ship);
10-9 Topics Inheritance Polymorphism Interfaces Packages
10-10 Packages A Java package is a collection of classes May or may not be related by inheritance Groups similar and interdependent classes together The Java API is composed of multiple packages The import statement is used to assert that a particular program will use classes from a particular package
10-11 Packages A programmer can define a package and add classes to it The package statement is used to specify that all classes defined in a file belong to a particular package The syntax of the package statement is: package package-name ; It must be located at the top of a file, and there can be only one package statement per file
10-12 Packages The classes must be organized in the directory structure such that they can be found when referenced by an import statement There is a CLASSPATH environment variable on each computer system that determines where to look for classes when referenced See Simple_IO and Simple_IO_Test.java
10-13 Packages The import statement specifies particular classes, or an entire package of classes, that can be used in that program Import statements are not necessary; a class can always be referenced by its fully qualified name in- line See Simple_IO_Test2.java If two classes from two packages have the same name and are used in the same program, they must be referenced by their fully qualified name
10-14 Packages As a rule of thumb, if you will use only one class from a package, import that class specifically If two or more classes will be used, use the * wildcard character in the import statement to provide access to all classes in the package See Simple_IO_Test3.java