+ int toInt()
Download
Report
Transcript + int toInt()
Lecture 9.4
Java Interfaces
Java does not support multiple inheritance.
... but there are times when multiple inheritance is essential.
The solution is an interface.
Interface Characteristics
1) Multiple interfaces can be inherited by the same class.
2) An interface cannot contain any method code (i.e. subclasses must
implement).
© 2006 Pearson Addison-Wesley. All rights reserved
9.4.2
Syntax template for implementing interfaces
public class SomeClass
implements Interface1,Interface2 {
// all methods from Interface1 and Interface2 must be coded.
...
}
OR
public class SomeClass extends ParentClass
implements Interface1,Interface2 {
// all methods from Interface1 and Interface2 must be coded.
// ParentClass is inherited.
...
}
© 2006 Pearson Addison-Wesley. All rights reserved
9.4.3
«interface»
Comparable
«query»
+ int compareTo(Object z)
A class that implements Comparable is used to support objects that can be
compared for greater than and equal to.
Comparable Interface Specifications
/** post: (this less_than z) implies result < 0
*
and (this equal_to z) implies result == 0
*
and (this greater_than z) implies result > 0 */
public int compareTo(Object z)
Note that the code for this method must
be supplied by the implementing class.
© 2006 Pearson Addison-Wesley. All rights reserved
9.4.4
Creating an interface is much like creating an abstract class.
• Interfaces use the work interface in place of class
• All interface methods have “;” in place of a body.
• Interfaces are stored in .java files with their name.
Example
Consider the need to store and manipulate natural numbers (integers
greater or equal to 1) using a variety of representations.
© 2006 Pearson Addison-Wesley. All rights reserved
9.4.5
«interface»
NaturalNumberInterface
«query»
+ int toInt()
+ String toString()
«update»
+ void set( int j )
+ void incrementByOne()
BigNumber
- byte[ ] digits
«constructor»
+ BigNumber()
«query»
+ int toInt()
+ String toString()
+ int digitCount()
«update»
+ void set( int j )
+ void incrementByOne()
© 2006 Pearson Addison-Wesley. All rights reserved
NaturalNumber
# SomeNumericType thisValue
«constructor»
+ NaturalNumber()
«query»
+ int toInt()
+ String toString()
«update»
+ void incrementByOne()
RomanNumber
«constructor»
+ RomanNumber()
«query»
+ String toString()
«update»
+ void setFromRoman( String s )
9.4.6
public interface NaturalNumberInterface {
public int toInt();
public String toString();
public void set( int j);
public abstract void incrementByOne();
}
public class NaturalNumber
implements NaturalNumberInterface {
protected int thisValue;
public NaturalNumber() {
thisValue = 1;
}
public int toInt() {
return thisValue;
}
public String toString() {
return “”+thisValue;
}
public void set(int j) {
thisValue = j;
}
public void incrementByOne() {
thisValue++;
}
}
© 2006 Pearson Addison-Wesley. All rights reserved
9.4.7
The following standard classes implement Comparable.
String
Byte, Character, Double, Float, Long, Integer, Short
You can also build your own…
public class Fraction implements Comparable {
protected int numer, denom;
public int compareTo(Object z) {
Fraction zFrac = (Fraction) z;
if (numer*zFrac.denom < denom*zFrac.numer)
return -1;
else if (numer*zFrac.denom == denom*zFrac.numer)
return 0;
else
return 1;
}
...
}
© 2006 Pearson Addison-Wesley. All rights reserved
9.4.8
Abstract Class
Inheritance syntax
extends
Interface
implements
Mark methods as “abstract”?
Yes
no
Non-deferred methods possible?
Yes
no
no
Yes
Subclasses must code
deferred?
Instance variables possible?
Can be inherited multiply?
© 2006 Pearson Addison-Wesley. All rights reserved
Yes
no
Yes, if initialized
Yes
9.4.9