Presentation is loading. Please wait.

Presentation is loading. Please wait.

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?

Similar presentations


Presentation on theme: "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?"— Presentation transcript:

1 Interfaces and Inner Classes

2 What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?

3 Definition from Type Theory  An interface is a “type”  A type is a collection of method specifications A “contract” A “contract” The methods a user can expect to call upon The methods a user can expect to call upon  That’s it!

4 Separating Interface and Implementation  Why is it a Good Thing?  How is it done?  Example: Figures 1-5 in Jan00

5 Separation Anxiety  To fully separate interface from implementation, we separate the contract from the class(es) that implement(s) it!

6 Java Interfaces  Similar to a class definition  Used mainly for function prototypes Abstract method declarations Abstract method declarations Public and abstract by default! Public and abstract by default!  Can also include: constants (static finals) constants (static finals) Nested classes and interfaces (covered later) Nested classes and interfaces (covered later)  Example: Next slide, Figure 6

7 interface Stack { void push(Object o) throws StackException; Object pop() throws StackException; Object top() throws StackException; int size(); }

8 class FixedStack implements Stack { // implementation unchanged // from Figure 1 … }

9 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

10 Implementation Inheritance  What happens with extends  The subclass inherits implementation: Data and method bodies Data and method bodies You’re stuck with a particular implementation You’re stuck with a particular implementation

11 Interface Inheritance  A commitment to implement a contract  No implementation is inherited  Disadvantage: No code sharing No code sharing  Advantage: No code commitment No code commitment Freedom to implement any way you want Freedom to implement any way you want

12 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

13 Multiple Inheritance  What kind?  Multiple inheritance of implementation is fraught with complexity Virtual base classes Virtual base classes Dominance rules Dominance rules  Multiple inheritance of interfaces just means you must implement all the methods in the hierarchy And that’s all it means And that’s all it means

14 class DynamicStack implements Stack, Persistent { // implementation as in Figure 4 // PLUS need to implement read and write }

15 Interfaces as Capabilities  Implements multiple interfaces  Interface names are often adjectives They describe capabilities They describe capabilities  Example: AbleTest.java

16 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 {…}

17 Interfaces in the Java Library  Comparable: public int compareTo(Object x)  Like C’s strcmp, returns: Negative, if this < x Negative, if this < x Zero if this.equals(x) Zero if this.equals(x) Positive, if this > x Positive, if this > x  You decide how the ordering works  Used throughout the library

18 Comparing Fractions compareTo( ) should return ad - bc

19 Interfaces in the Java Library  Cloneable For copying objects For copying objects Kind of dorky Kind of dorky  Serializable For automatic object storage and retrieval For automatic object storage and retrieval  Collection Basic contract for collections Basic contract for collections

20 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 Benefit: Can have multiple iterators simultaneously Implemented as nested classes Implemented as nested classes

21 Iterator Example  MySequence.java An expandable array of Object An expandable array of Object  MyIterator implements java.util.Iterator  MySequence.getIterator( ) returns a MyIterator object

22 Issues with MySequence.java  The class MyIterator has more visibility than it needs Package access (we want private) Package access (we want private) Top-level classes can’t be private (or protected)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 They don’t care what type actually does it  Solution: nest MyIterator inside MySequence

23 Nested Classes  Can define classes and interfaces within other classes and interfaces  Two flavors: Static (like nested classes in C++) Static (like nested classes in C++) Non-static (different!) Non-static (different!) Also called “inner classes”Also called “inner classes”  Can also define classes inside of a method “local classes” “local classes”

24 Static Nested Classes  Just like C++  Just a scoping mechanism  class A { static class B {…} } A.B b = new A.B();

25 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 Usually private  Used a lot in AWT/Swing

26 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(); = outer.new Inner(); See page 230 of Core Java

27 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 Just like “this” is implicit in non-static methods Any references to MySequence fields is resolved automatically Any references to MySequence fields is resolved automatically  “data” == “MySequence.this.data”

28 Local Inner Classes  Defined inside a method Not visible outside the method Not visible outside the method  Can only access final locals in the enclosing method See p. 233 in Core Java See p. 233 in Core Java  Example: MySequence3.java

29 Anonymous Inner Classes  The name MyIterator is only used in one place Hardly seems worth a separate name! 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

30 Marker Interfaces  Interfaces with no definitions!  Merely to “color” or “tag” a class  Cloneable  Serializable Will cover in I/O section Will cover in I/O section

31 Cloning  Meant to replace copy constructors For “deep copies” For “deep copies” Somewhat problematic, but widely used! Somewhat problematic, but widely used!  Must Implement Cloneable  Must override Object.clone( ) But make it public! (It’s protected in Object) But make it public! (It’s protected in Object) Call super.clone() first! (To get the right type) 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 Throws CloneNotSupportedException  Example: Figures 7-9

32 Cloning Policy  1) Support it As just described As just described  2) Let subclasses support it Don’t implement Cloneable Don’t implement Cloneable But provide a protected clone if Object.clone() isn’t deep enough But provide a protected clone if Object.clone() isn’t deep enough  3) Forbid cloning Provide a clone( ) that unconditionally throws CloneNotSupportedException Provide a clone( ) that unconditionally throws CloneNotSupportedException


Download ppt "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?"

Similar presentations


Ads by Google