Interfaces Outline Generic interface versus Java interface What

  • Slides: 6
Download presentation
Interfaces • Outline: – Generic interface versus Java interface – What a (Java) interface

Interfaces • Outline: – Generic interface versus Java interface – What a (Java) interface is: • • Its syntax What it says What it does NOT say How it is used, why it is useful – How interfaces are used in Word. Games • UML class diagram for Word. Games – Word. Games • An application of interfaces Fundamentals of Software Development 1 1

Generic interfaces • Generic computer-science interface – The “face” that one entity shows others:

Generic interfaces • Generic computer-science interface – The “face” that one entity shows others: • What services it provides • What information it expects – Physical examples: • • • Computer Interfaces Stethoscope Telephone Camera Toaster Fundamentals of Software Development 1 2

Why have interfaces? • A Java interface is the “face” that one class shows

Why have interfaces? • A Java interface is the “face” that one class shows others – An interface contains the signature but not body for each method – Any class that implements an interface must provide the methods listed in the interface • So an interface specifies a contract Example of a Java interface public interface Clock. Radio { public Time get. Time(); public void set. Time(Time x, Station s); } Fundamentals of Software Development 1 3

Why have interfaces? public interface Clock. Radio { public Time get. Time(); public void

Why have interfaces? public interface Clock. Radio { public Time get. Time(); public void set. Time(Time x, Station s); } • Java interfaces are important because: – They provide for software reuse. For example: • • • Interfaces can I provide a String. Transformable interface You provide classes that implement String. Transformable (and should) be used as types Our separately-written code works together seamlessly! – They provide genericity. For example: • My code declares each of your things as a String. Transformable thing • You provide various implementations of String. Transformable – e. g. Capitalizer, Name. Dropper, Ubby. Dubber, … • My code is generic – it doesn’t care what implementation of String. Transformable you supply! Fundamentals of Software Development 1 4

UML class diagram for Word. Games All our stuff The String. Transformable interface is

UML class diagram for Word. Games All our stuff The String. Transformable interface is how our code knows how to “connect” to your code <<interface> String. Transformable -------------transform(String) : String Capitalizer Fundamentals of Software Development 1 Name. Dropper xxx … xxx Questions on this important idea? 5

Java Interfaces: Summary public interface Clock. Radio { public Time get. Time(); public void

Java Interfaces: Summary public interface Clock. Radio { public Time get. Time(); public void set. Time(Time x, Station s); } • A Java interface is the “face” that one class shows others – An interface contains the signature but not body for each method – Any class that implements an interface must provide the methods listed in the interface • So an interface provides a contract • Java interfaces are important because they provide for: – Software reuse – Genericity Fundamentals of Software Development 1 From now on, “interface” means “Java interface” to us 6