java coding oop_3 david davenport computer eng. dept., bilkent university ankara - turkey. email:...

13
Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: [email protected] Some important Java interfaces + Inner classes

Upload: jared-doyle

Post on 14-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

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

Java Coding OOP_3

David Davenport

Computer Eng. Dept.,Bilkent UniversityAnkara - Turkey.

email: [email protected]

Some important Java interfaces + Inner classes

Page 2: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

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.

Page 3: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

SOME JAVA INTERFACES…

Page 4: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

Java Interfaces Design by interface gives flexibility

The Java API has a lot of interfaces Iterator Comparable Serializable & a multitude for

GUI event-handling!

Page 5: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

Java’s Iterator Interface boolean hasNext() Object next() void remove()

// Scanner class implements Iterator

tokens = new Scanner( “To be or not to be”);

while ( tokens.hasNext() )

System.out.println( tokens.next() );

To iterate (to go) through a collection,

processing each element once and

once only

Page 6: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

Java’s Iterator Interface boolean hasNext() Object next() void remove()

// given an ArrayList, list

Iterator x = list.iterator();

while ( x.hasNext() )

System.out.println( x.next() );

Surprisingly, ArrayList does not implement

Iterator.

It implements Iterable!

Iterator iterator()

Page 7: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

The Enumeration Interface boolean hasNextElement() E nextElement()

// StringTokenizer

tokens = new StringTokenizer(

“To be or not to be”);

while ( tokens.hasMoreElements() )

System.out.println( tokens.nextElement() );

Similar to, but older than Iterator.

Has different names & does not have remove

method.

Examples include StringTokenizer & Vector (which also has Iterator!)

Page 8: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

Java’s Comparable Interface

int compareTo( Object o)compare this object with o and return negative, zero, positiveto indicate <, =, > respectively!

// Assuming x implements Comparable

// e.g. String

if ( x.compareTo( y) > 0 )

exchange( x, y);

Page 9: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

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.

Page 10: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

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.

Page 11: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

INNER CLASSES…

Page 12: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

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)

Page 13: Java Coding OOP_3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. email: david@bilkent.edu.tr Some important Java interfaces +

Inner Classes (cont.)

Anonymous Inner classes Create in-line (on the fly!) Useful when only a single object needed

& class not otherwise reusable.

variation of new statement…

className x = new superClass_or_interface() {

// the prop’s & methods

}