specifies a set of methods (i.e., method headings) that any class that implements that interface...

36

Upload: emmalee-endsley

Post on 14-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)
Page 2: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.

An interface is a type (but is not a class). Interface can be parameter types.

Java’s way of approximating multiple inheritance.

Page 3: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Abstract or concrete classes may implement interfaces.

Page 4: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

To implement an interface, a concrete class must do:

1. State “implements InterfaceName” or “implements InterfaceName1, …, InterfaceNamen”

2. You must implement all of the method headings listed in the definition(s) of the interface(s).

Page 5: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

To implement an interface, an abstract class must do:

1. State “implements InterfaceName” or “implements InterfaceName1, …, InterfaceNamen”

2. You must either implement all of the method headings listed in the definition(s) of the interface(s), or you must define as abstract the method headings in the interface(s).

Page 6: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

An interface B may extend an interface A and specify additional method headings.

Any concrete class that implements the derived interface B must implement all of the methods in both interfaces A and B.

Page 7: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)
Page 8: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

See http://java.sun.com/j2se/1.3/docs/api/java/lang/Comparable.html

Used for sorting. If things can be compared, they can be sorted.

One method: public int compareTo ( Object other );

-1 means that this comes before (is less than) other 0 means that this and other are equal +1 means that this comes after (is greater than)

other

Page 9: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Properties:1. Irreflexive: For no object o does o come before

o. reflexive: x R x xA irreflexive: (x R x) xA

2. Trichotomy: For any two object o1 and o2, one and only one of the following holds true:

o1 comes before o2, or o1 comes after o2, or o1 equals o2.

3. Transitivity: If o1 comes before o2 and o2 comes before o3, then o1 comes before o3.

Page 10: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

if (a.compareTo(b) < 0) {…

} else if (a.compareTo(b) == 0) {…

} else { //must be a.compareTo(b)>0…

}

Page 11: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

See http://java.sun.com/j2se/1.3/docs/api/java/lang/Double.html

See http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html

Page 12: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Is the following a suitable implementation of the Comparable interface?

public class Double2 implements Comparable { private double value; public Double2 ( double theValue ) { value = theValue; } public int compareTo ( Object other ) { return -1; } public double doubleValue ( ) { return value; }}

You can think of the underlying “comes before” relationship as saying that for any objects d1 and d2, d1 comes before d2.

Page 13: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Suppose you have a class Circle that represents circles all of whose centers are at the same point. (To make it concrete you can take the circles to be in the usual x,y plane and all have their centers at the origin.) Suppose there is a boolean valued method inside of the class Circle such that, for circles c1 and c2, c1.inside(c2) returns true if c1 is completely inside of c2 (and c2 is not the same as c1). Is the following a total ordering?

c1 comes before c2 if c1 is inside of c2

(that is, c1.inside(c2) returns true).

You could represent objects of the class Circle by a single value of type double that gives the radius of the circle, but the answer does not depend on such details.

Page 14: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Constants may be defined in interfaces but ...

Not really in the spirit of an interface

Must be public static final (and will be, even if omitted)

No instance variables in interfaces

Page 15: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)
Page 16: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Implement two interfaces which:

1. have conflicting constants, or

2. have overloaded methods with different return types

Page 17: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Implement two interfaces which have conflicting constants.class TestInterface implements I1, I2 { public static void main ( String[] args ) { System.out.println( A ); }}

interface I1 { int A = 100;}

interface I2 { int A = 500;}

Compiler error – ambiguous.

Page 18: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Implement two interfaces which have overloaded methods with different return types.class TestInterface implements I1, I2 { public static void main ( String[] args ) { } public int f ( ) { return 0; }}

interface I1 { public int f ( );}

interface I2 { public String f ( );}

Compiler error – TestInterface is not abstract and does not override abstract method f() in I2

Page 19: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Implement two interfaces which have overloaded methods with different return types.class TestInterface implements I1, I2 { public static void main ( String[] args ) { } public int f ( ) { return 0; } public String f ( ) { return null; }}interface I1 { public int f ( );}interface I2 { public String f ( );}

Compiler error – f() is already defined in TestInterface

Page 20: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Implement two interfaces which have overloaded methods with same return types.class TestInterface implements I1, I2 { public static void main ( String[] args ) { } public int f ( ) { return 0; }}interface I1 { public int f ( );}interface I2 { public int f ( );}

OK

Page 21: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)
Page 22: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Note that the Object class has a clone() method.

See http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html

protected Object clone ( ) Creates and returns a copy of this object. Note that it’s protected.

Interesting! Let’s give it a try.

Page 23: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

public class Test {

private int x=5; public static void main ( String args[] ) { Test t1 = new Test(); t1.x = 12; Test t2 = (Test)t1.clone(); System.out.println( t2.x ); }

}

Compiler error:Test.java:8: unreported exception java.lang.CloneNotSupportedException; must be caught or declared to be thrown Test t2 = (Test)t1.clone(); ^1 error

Page 24: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

public class Test {

private int x=5; public static void main ( String args[] ) { Test t1 = new Test(); t1.x = 12; Test t2 = null; try { t2 = (Test)t1.clone(); } catch (Exception e) { System.out.println( "error: " + e ); } System.out.println( t2.x ); }}

Compiles OK. But runtime error:

error: java.lang.CloneNotSupportedException: TestException in thread "main" java.lang.NullPointerException at Test.main(Test.java:14)

Page 25: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Has no headings.

Has no constants.

So what does it do? Magic!

Page 26: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

public class Test implements Cloneable {

private int x=5; public static void main ( String args[] ) { Test t1 = new Test(); t1.x = 12; Test t2 = null; try { t2 = (Test)t1.clone(); } catch (Exception e) { System.out.println( "error: " + e ); } System.out.println( t2.x ); }}

Compile OK.Runs OK.Outputs:

12

Page 27: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Object.clone() Exact, bit-by-bit copy May cause privacy leaks

To avoid privacy leaks, your clone() method should:1. Invoke super.clone(), and2. Create new instances of mutable types

Page 28: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

Simplest case1. You have no mutable instance variables (but

you may have primitive and/or immutable instance variables), and

2. You are derived from Object.

Page 29: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

public class Test implements Cloneable {

private int x=5; public Object clone ( ) { try { return super.clone(); } catch (Exception e) { return null; } } public static void main ( String args[] ) { Test t1 = new Test(); t1.x = 12; Test t2 = (Test)t1.clone(); System.out.println( t2.x ); //outputs 12 }}

Compiles OK.

Runs w/out error/exception.

Don’t have to trouble ourselves in main() with exceptions.

Outputs 12 as expected.

Page 30: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

import java.util.Date;import java.awt.Point;

public class Point3D extends Point implements Cloneable{ private int z = 0; private Date date = new Date(); public Object clone ( ) { Point3D copy = null; try { copy = (Point3D)super.clone(); } catch (Exception e) { return null; } return copy; }

public static void main ( String args[] ) {

Point3D p1 = new Point3D(); Point3D p2 =

(Point3D)p1.clone();

System.out.println( p2.date ); //month becomes March p1.date.setMonth( 2 ); System.out.println( p2.date ); }

}This example:(a)Clones p1 into p2.(b)Prints p2.(c)Changes p1.(d)Prints p2 again (which has now magically changed).

Page 31: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

import java.util.Date;import java.awt.Point;

public class Point3D extends Point implements Cloneable{ private int z = 0; private Date date = new Date(); public Object clone ( ) { Point3D copy = null; try { copy = (Point3D)super.clone(); } catch (Exception e) { return null; } copy.date = (Date)date.clone(); return copy; }

public static void main ( String args[] ) {

Point3D p1 = new Point3D(); Point3D p2 =

(Point3D)p1.clone();

System.out.println( p2.date ); //month becomes March p1.date.setMonth( 2 ); System.out.println( p2.date ); }

}

Page 32: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)
Page 33: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

1. ActionListener

2. MouseListener

3. MouseMotionListener

Mention sync vs. async event handling.

Page 34: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

See http://java.sun.com/j2se/1.5.0/docs/api/java/awt/event/ActionListener.html.

One method: void actionPerformed ( ActionEvent e );

Invoked when an action occurs such as a button press.

Page 35: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

void mouseClicked ( MouseEvent e ) Invoked when the mouse button has been clicked

(pressed and released) on a component. void mousePressed ( MouseEvent e )

Invoked when a mouse button has been pressed on a component.

void mouseReleased ( MouseEvent e ) Invoked when a mouse button has been released on a

component. void mouseEntered ( MouseEvent e )

Invoked when the mouse enters a component. void mouseExited ( MouseEvent e )

Invoked when the mouse exits a component.

Page 36: Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class)

void mouseDragged ( MouseEvent e ) Invoked when a mouse button is pressed on a

component and then dragged. MOUSE_DRAGGED events will continue to be delivered to the component where the drag originated until the mouse button is released (regardless of whether the mouse position is within the bounds of the component).

Due to platform-dependent Drag&Drop implementations, MOUSE_DRAGGED events may not be delivered during a native Drag&Drop operation.

void mouseMoved ( MouseEvent e ) Invoked when the mouse cursor has been moved onto

a component but no buttons have been pushed.