Download presentation
Presentation is loading. Please wait.
1
What is an Interface? 4 Common in everyday life. –Movie theaters, TVs, VCRs, etc. 4 Java interface definitions are simple: –Method headers. –Field definitions ( public static final ).
2
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; }
3
Implementing an Interface class PanasonicVCR implements VCR {... public boolean eject(){ if(getStatus() != NOTAPE){ setStatus(NOTAPE); return true; } else{ return false; }... }
4
The Iterator interface public interface Iterator { public boolean hasNext(); public Object next() throws NoSuchElementException; } LinkedList public ListIterator listIterator(int index) ListIteratorlistIterator Iterator it = l.listIterator(0); while (it.hasNext()){ Object elm = it.next(); … }
5
The Enumeration Interface public interface Enumeration { public boolean hasMoreElements(); public Object nextElement() throws NoSuchElementException; } Hashtable public Enumeration elements()Enumeration public Enumeration keys()Enumeration
6
Packages 4 What is a package? –A package is an encapsulation of classes possibly stored in multiple files. 4 Why packages? –Provide another level of modularity –Provide another level of protection
7
Defining and Using Packages 4 Defining packages 4 Using packages 4 Managing Java source and class files package packageName; import packageName.ClassName; import packageName.*;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.