Title: Interfaces and Inner Classes
1Interfaces and Inner Classes
2What is an Interface?
- What is presented to the user?
- The public part of a class?
- What is the substance of an interface?
3Definition 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
- Thats it!
4Separating Interface and Implementation
- Why is it a Good Thing?
- How is it done?
- Example Figures 1-5 in Jan00
5Separation Anxiety
- To fully separate interface from implementation,
we separate the contract from the class(es) that
implement(s) it!
6Java 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
7interface Stack void push(Object o)
throws StackException Object pop()
throws StackException Object top()
throws StackException int size()
8class FixedStack implements Stack //
implementation unchanged // from Figure 1
9Advantage of Interfaces(over inheritance)
- Any class that implements the interface (i.e.,
provides the contracts functionality) can be
used - Not constrained by subclassing
10Implementation Inheritance
- What happens with extends
- The subclass inherits implementation
- Data and method bodies
- Youre stuck with a particular implementation
11Interface 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
12Interfaces 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
13Multiple 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 thats all it means
14class DynamicStack implements Stack,
Persistent // implementation as in Figure
4 // PLUS need to implement read and write
15Interfaces as Capabilities
- Implements multiple interfaces
- Interface names are often adjectives
- They describe capabilities
- Example AbleTest.java
16Sub- 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
17Interfaces in the Java Library
- Comparablepublic int compareTo(Object x)
- Like Cs strcmp, returns
- Negative, if this lt x
- Zero if this.equals(x)
- Positive, if this gt x
- You decide how the ordering works
- Used throughout the library
18Comparing Fractions
compareTo( ) should return ad - bc
19Interfaces in the Java Library
- Cloneable
- For copying objects
- Kind of dorky
- Serializable
- For automatic object storage and retrieval
- Collection
- Basic contract for collections
20Iterators
- A Design Pattern for traversing collections
- java.util.Iterator interfacepublic boolean
hasNext( )public Object next( )public void
remove( ) - Collections typically implement Iterator
- Benefit Can have multiple iterators
simultaneously - Implemented as nested classes
21Iterator Example
- MySequence.java
- An expandable array of Object
- MyIterator implements java.util.Iterator
- MySequence.getIterator( ) returns a MyIterator
object
22Issues with MySequence.java
- The class MyIterator has more visibility than it
needs - Package access (we want private)
- Top-level classes cant be private (or protected)
- Clients only care about the Iterator interface
being implemented - They dont care what type actually does it
- Solution nest MyIterator inside MySequence
23Nested 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
24Static Nested Classes
- Just like C
- Just a scoping mechanism
- class A static class B A.B b new A.B()
25Inner 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
26Special 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
27MySequence2.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
28Local 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
29Anonymous 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
30Marker Interfaces
- Interfaces with no definitions!
- Merely to color or tag a class
- Cloneable
- Serializable
- Will cover in I/O section
31Cloning
- Meant to replace copy constructors
- For deep copies
- Somewhat problematic, but widely used!
- Must Implement Cloneable
- Must override Object.clone( )
- But make it public! (Its 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 - Throws CloneNotSupportedException
- Example Figures 7-9
32Cloning Policy
- 1) Support it
- As just described
- 2) Let subclasses support it
- Dont implement Cloneable
- But provide a protected clone if Object.clone()
isnt deep enough - 3) Forbid cloning
- Provide a clone( ) that unconditionally throws
CloneNotSupportedException