Transcript Interfaces

Interfaces and Inner
Classes
What is an Interface?
 What
is “presented to the user”?
 The public part of a class?
 What is the substance of an interface?
Definition from Type Theory
 An
interface is a “type”
 A type is a collection of method
specifications


A “contract”
The methods a user can expect to call upon
 That’s
it!
Separating Interface and
Implementation
 Why
is it a Good Thing?
 How is it done?
 Example: Figures 1-5 in Jan00
Separation Anxiety
 To
fully separate interface from
implementation, we separate the contract
from the class(es) that implement(s) it!
Java Interfaces
 Similar
to a class definition
 Used mainly for function prototypes


Abstract method declarations
Public and abstract by default!
 Can


also include:
constants (static finals)
Nested classes and interfaces (covered later)
 Example:
Next slide, Figure 6
interface Stack
{
void push(Object o)
throws StackException;
Object pop()
throws StackException;
Object top()
throws StackException;
int size();
}
class FixedStack implements Stack
{
// implementation unchanged
// from Figure 1 …
}
Advantage of Interfaces
(over inheritance)
 Any
class that implements the interface
(i.e., provides the contract’s functionality)
can be used
 Not constrained by subclassing
Implementation Inheritance
 What
happens with extends
 The subclass inherits implementation:


Data and method bodies
You’re stuck with a particular implementation
Interface Inheritance
 A commitment
to implement a contract
 No implementation is inherited
 Disadvantage:

No code sharing
 Advantage:


No code commitment
Freedom to implement any way you want
Interfaces vs. Abstract Classes
 Use Abstract
Classes when there is some
implementation to share
 In C++, an abstract class with only pure
virtual functions and no implementation
behaves as an interface
Multiple Inheritance
 What
kind?
 Multiple inheritance of implementation is
fraught with complexity


Virtual base classes
Dominance rules
 Multiple
inheritance of interfaces just
means you must implement all the
methods in the hierarchy

And that’s all it means
class DynamicStack implements Stack, Persistent
{
// implementation as in Figure 4
// PLUS need to implement read and write
}
Interfaces as Capabilities
 Implements
multiple interfaces
 Interface names are often adjectives

They describe capabilities
 Example: AbleTest.java
Sub- and Super-Interfaces
 An
interface can extend another interface
 The net result is just the union of all the
method specifications
interface PersistentStack extends Stack, Persistent
{}
class DynamicStack implements PersistentStack {…}
Interfaces in the Java Library
 Comparable:
public int compareTo(Object x)
 Like



C’s strcmp, returns:
Negative, if this < x
Zero if this.equals(x)
Positive, if this > x
 You
decide how the ordering works
 Used throughout the library
Comparing Fractions
a c
b d
 ad
bc
(b  0, d  0)
compareTo( ) should return ad - bc
Interfaces in the Java Library
 Cloneable


For copying objects
Kind of dorky
 Serializable

For automatic object storage and retrieval
 Collection

Basic contract for collections
Iterators
 A Design
Pattern for traversing collections
 java.util.Iterator interface:
public boolean hasNext( );
public Object next( );
public void remove( );
 Collections


typically implement Iterator
Benefit: Can have multiple iterators
simultaneously
Implemented as nested classes
Iterator Example
 MySequence.java

An expandable array of Object
 MyIterator
implements java.util.Iterator
 MySequence.getIterator( ) returns a
MyIterator object
Issues with MySequence.java
 The
class MyIterator has more visibility
than it needs

Package access (we want private)
• Top-level classes can’t be private (or protected)
 Clients
only care about the Iterator
interface being implemented

They don’t care what type actually does it
 Solution:
nest MyIterator inside
MySequence
Nested Classes
 Can
define classes and interfaces within
other classes and interfaces
 Two flavors:


Static (like nested classes in C++)
Non-static (different!)
• Also called “inner classes”
 Can
also define classes inside of a
method

“local classes”
Static Nested Classes
 Just
like C++
 Just a scoping mechanism

class A {
static class B {…}
}
A.B b = new A.B();
Inner Classes
 Objects
of inner classes only exist in
connection with an instance of their
containing class(es)
 They have an invisible link to the object of
the containing class
 They can be declared with any access
specifier

Usually private
 Used
a lot in AWT/Swing
Special Syntax
 Refer
to variable in outer class:
Outer.this.varName
 Make
an object of inner class:
Outer outer = new Outer();
Outer.Inner inner
= outer.new Inner();
See page 230 of Core Java
MySequence2.java
 MyIterator
is an inner class
 A MyIterator object has an implicit
MySequence object that “owns” it


Just like “this” is implicit in non-static methods
Any references to MySequence fields is
resolved automatically
 “data”
== “MySequence.this.data”
Local Inner Classes
 Defined

inside a method
Not visible outside the method
 Can
only access final locals in the
enclosing method

See p. 233 in Core Java
 Example:
MySequence3.java
Anonymous Inner Classes
 The
name MyIterator is only used in one
place

Hardly seems worth a separate name!
 Can
define an unnamed class “on-the-fly”
instead in the return expression in
MySequence.getIterator( )
 Can have no named constructors
 Example: MySequence4.java
Marker Interfaces
 Interfaces
with no definitions!
 Merely to “color” or “tag” a class
 Cloneable
 Serializable

Will cover in I/O section
Cloning

Meant to replace copy constructors




Must Implement Cloneable
Must override Object.clone( )



But make it public! (It’s protected in Object)
Call super.clone() first! (To get the right type)
It is an error to call clone( ) for a class that does
not implement Cloneable


For “deep copies”
Somewhat problematic, but widely used!
Throws CloneNotSupportedException
Example: Figures 7-9
Cloning Policy
 1)

 2)


 3)

Support it
As just described
Let subclasses support it
Don’t implement Cloneable
But provide a protected clone if Object.clone()
isn’t deep enough
Forbid cloning
Provide a clone( ) that unconditionally throws
CloneNotSupportedException