AirplaneInterfaces

AirplaneInterfaces - Sample Use Of Java Interfaces Interface

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Sample Use Of Java Interfaces Interface EngineInformation public interface EngineInformation { public String getManufacturer(); public int getHorsePower(); } One Way Application Programs Use Interfaces Let's say we're doing military applications that retrieve and present information about equipment that the military has. We would like to STANDARDIZE our access to the same kinds of objects, engines for example, so we don't have to use different access methods to extract the same kind of information from different kinds of engines. So we make up an interface we would like to use (a very simple example is shown above) and ask all the programmers of engine classes to implement it. Or we ask the engine programmers to get together and make up an interface and provide it to the application programmers. Either way, someone creates an interface as shown above. As you can see, the interface just declares methods, it doesn't implement them. The name of the file would be EngineInformation.java. It would be compiled to generate EngineInformation.class. JetEngine class and TankEngine class would implement the interface, for example, as follows. (When the class declares "implements EngineInformation" on the class declaration line, the compiler ensures that the class in fact implements the methods declared in the interface file.) At run time, the object (a JetEngine or a TankEngine) also takes on EngineInformation as a type , just like it has the type of its class and all of its parent classes. Implementing class JetEngine class JetEngine implements EngineInformation {
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
public void startEngine() {/* implementation */} public void stopEngine() {/* implementation */} public String getManufaturer() {return "Westinghouse";} public int getHorsePower() {return 10000;} public void reverseEngineBlades() {/* implementation */} public void openEngineCovers() {/* implementation */} } Implementing class TankEngine class TankEngine implements EngineInformation { public void startEngine() {/* implementation */} public void stopEngine() {/* implementation */} public String getManufaturer() {return "Ford";} public int getHorsePower()
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

AirplaneInterfaces - Sample Use Of Java Interfaces Interface

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online