1 / 15

Java Coding OOP_3

Java Coding OOP_3. Some important Java interfaces + Inner classes. David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr. IMPORTANT…. Students…

rmilligan
Download Presentation

Java Coding OOP_3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Coding OOP_3 Some important Java interfaces + Inner classes David Davenport Computer Eng. Dept., Bilkent UniversityAnkara - Turkey. email: david@bilkent.edu.tr

  2. IMPORTANT… • Students… This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! • Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an email saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you,David.

  3. Some Java Interfaces…

  4. Java Interfaces • Design by interface gives flexibility • The Java API has a lot of interfaces • Iterator • Comparable • Serializable • & a multitude for GUI event-handling!

  5. Java’s Iterator Interface • boolean hasNext() • Object next() • void remove() To iterate (to go) through a collection, processing each element once and once only // Scanner class implements Iterator tokens = new Scanner( “To be or not to be”); while ( tokens.hasNext() ) System.out.println( tokens.next() );

  6. Java’s Iterator Interface • boolean hasNext() • Object next() • void remove() Surprisingly, ArrayList does not implement Iterator. It implements Iterable! Iterator iterator() // given an ArrayList called list Iterator x = list.iterator(); while ( x.hasNext() ) System.out.println( x.next() ); why?

  7. The Enumeration Interface • boolean hasMoreElements() • E nextElement() Similar to, but older than Iterator. Has different names & does not have remove method. Examples include StringTokenizer & Vector (which also has Iterator!) // StringTokenizer tokens = new StringTokenizer( “To be or not to be”); while ( tokens.hasMoreElements() ) System.out.println( tokens.nextElement() );

  8. Java’s Comparable Interface • int compareTo( Object o) compare this object with o and return negative, zero, positive to indicate <, =, > respectively! // Assuming x implements Comparable // e.g. String if ( x.compareTo( y) > 0 ) exchange( x, y);

  9. Java’s Serialization Interface • Allows Java objects to be converted to/from a stream of bytes! • Rather special since you do not need to write any methods, merely • Make class implement Serializable • Have a default constructor • Ensure all properties are Serializable • Check the Java API documentation for details and example code.

  10. Interface notes • Can’t create objects of interface type, only a type that implements it • Interfaces can extend interfaces (but not classes) to form a hierarchy separate from class one • Start design with interfaces! • Java provides “instanceof” operator to test object type (but use very sparingly) • Cloneable is another common interface.

  11. Inner Classes…

  12. Inner Classes • Nested or Inner classes • are defined inside other classes • have direct access to outer class • can be named or anonymous • Named Inner classes • Simply define one class inside another!(generates outer$inner.class files)

  13. Anon. Inner Classes • Anonymous Inner classes • Create in-line (on the fly!) • Useful when only a single object needed & class not otherwise reusable. • variation of new statement… Note: either implement an interface or extend a class, not both! className x = new superClass_or_interface() { // the prop’s & methods }

  14. Anon. Inner Classes (cont.) public interface ActionListener { publicvoid actionPerformed( ActionEvent e);} ActionListener al = new ActionListener() { publicvoid actionPerformed( ActionEvent e) { System.out.println( "Button Pressed!"); } }; myButton.addActionListener( al);

  15. Anon. Inner Classes (cont.) ActionListener al = new ActionListener() { publicvoid actionPerformed( ActionEvent e) { System.out.println( "Button Pressed!"); } }; myButton.addActionListener( al); myButton.addActionListener( new ActionListener() { publicvoid actionPerformed( ActionEvent e) { System.out.println( "Button Pressed!"); } }); Java 8 Lambda function(shorthand) Not in CS102, yet! myButton.addActionListener( e -> System.out.println( "Button Pressed!") );

More Related