Download presentation
Presentation is loading. Please wait.
Published byMagdalena Hayles Modified over 9 years ago
1
OOP with Java, David J. Barnes Interfaces1 A set of features offered by a class. Often offered by more than one class. –Iterator : Returned by the iterator method of ArrayList and LinkedList. Alternative underlying implementations. –Used externally in an identical way. –What kept separate from how.
2
OOP with Java, David J. Barnes Interfaces2 What is an Interface? Common in everyday life. –Movie theaters, TVs, VCRs, etc. Java interface definitions are simple: –Method headers. –Field definitions ( public static final ).
3
OOP with Java, David J. Barnes Interfaces3 An Interface Definition interface VCR { public boolean loadTape(String tapeName); public boolean eject(); public boolean play(); public boolean pause(); public boolean stop(); public boolean ff(); public boolean rew(); public int getStatus(); public static final int PLAYING = 0, PAUSED = 1, STOPPED = 2, FORWARD = 3, BACKWARD = 4, NOTAPE = 5; }
4
OOP with Java, David J. Barnes Interfaces4 Implementing an Interface class NoFrillsVCR implements VCR {... public boolean eject(){ if(getStatus() != NOTAPE){ setStatus(NOTAPE); return true; } else{ return false; }... }
5
OOP with Java, David J. Barnes Interfaces5 Interface Fields Field definitions are not repeated in defining classes. Use unqualified in implementing classes. Fields accessible in different ways: –Via the interface name: VCR.STOPPED –Via an implementing class name: NoFrillsVCR.STOPPED –Via an instance reference: cheap.STOPPED
6
OOP with Java, David J. Barnes Interfaces6 Multiple Implementations One of the main reasons for creating an interface. No limit on the number of implementing classes. Implementations may add further features. –Variable playing speed, for instance. –These are not part of the interface.
7
OOP with Java, David J. Barnes Interfaces7 Polymorphism A method that knows about a particular interface may interact with an object of any of the implementing classes. It will not know about additional functionality: – e.g., variable playing speed. The instanceof operator, and a cast provide access to class-specific information.
8
OOP with Java, David J. Barnes Interfaces8 The instanceof Operator class VideoTester { private boolean testVCR(VCR video,String tape){... if(video instanceof UpMarketVCR){ // Test its extra features. UpMarketVCR betterVCR = (UpMarketVCR) video; // Play at twice the normal speed. betterVCR.setSpeed(2); }... }
9
OOP with Java, David J. Barnes Interfaces9 Interfaces without Methods A collecting point for related values: interface PieceNames { public static int Pawn = 0, Rook = 1, Knight = 2, Bishop = 3, Queen = 4, King = 5; } Similar to enumerated types, but no type checking. int piece = PieceNames.King; int badPiece = piece+1; // Nonsense.
10
OOP with Java, David J. Barnes Interfaces10 Iterator and Enumeration public interface Iterator { public boolean hasNext(); public Object next() throws NoSuchElementException; public void remove() throws IllegalStateException, UnsupportedOperationException; } public interface Enumeration { public boolean hasMoreElements(); public Object nextElement() throws NoSuchElementException; }
11
OOP with Java, David J. Barnes Interfaces11 Comparable and Comparator public interface Comparable { public int compareTo(Object o) throws ClassCastException; } public interface Comparator { public int compare(Object o1,Object o2) throws ClassCastException; public boolean equals(Object o); } Instances of implementing classes can be sorted.
12
OOP with Java, David J. Barnes Interfaces12 FileFilter and FilenameFilter public interface FileFilter { // Should the given path name be accepted? public boolean accept(File pathname); } public interface FilenameFilter { // Should name in directory be accepted? public boolean accept(File directory,String name); } Select acceptable/required files.
13
OOP with Java, David J. Barnes Interfaces13 Further Interface Issues Empty interface. –Can act as a marker ( Cloneable ). Implementing Multiple Interfaces. –A form of multiple inheritance. –Very common in GUI applications. –Conflict resolution required: Identical method names or field names.
14
OOP with Java, David J. Barnes Interfaces14 Conflict Resolution Common methods with different argument lists are overloaded. Common methods with identical headers have a single implementation. –This has to be meaningful for both interfaces. Checked exception lists must be reduced to the common subset. Common field names must be qualified.
15
OOP with Java, David J. Barnes Interfaces15 Serializable An empty interface to support persistence: object serialization. Serializable objects are manipulated via ObjectInputStream and ObjectOutputStream objects. transient fields are not serialized. Externalizable provides finer control.
16
OOP with Java, David J. Barnes Interfaces16 Review An interface contains only method signatures (headers) and/or fields. Fields are all public static final. Interfaces are usually implemented by multiple classes. Polymorphism allows any implementing instances to be typed as the interface type.
17
OOP with Java, David J. Barnes Interfaces17 Review (cont.) The instanceof operator allows the dynamic type of an object to be determined. Interfaces without methods are often used to collect related constant values. An empty interface may be used as a marker. There are many standard interfaces, e.g., Iterator.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.