Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved 9.4.2 Java does not support multiple inheritance. Interface Characteristics...

Similar presentations


Presentation on theme: "Lecture 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved 9.4.2 Java does not support multiple inheritance. Interface Characteristics..."— Presentation transcript:

1 Lecture 9.4 Java Interfaces

2 © 2006 Pearson Addison-Wesley. All rights reserved 9.4.2 Java does not support multiple inheritance. Interface Characteristics... but there are times when multiple inheritance is essential. The solution is an interface. 2) An interface cannot contain any method code (i.e. subclasses must implement). 1) Multiple interfaces can be inherited by the same class.

3 © 2006 Pearson Addison-Wesley. All rights reserved 9.4.3 Syntax template for implementing interfaces public class SomeClass implements Interface1,Interface2 { // all methods from Interface1 and Interface2 must be coded.... } 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.... } public class SomeClass extends ParentClass implements Interface1,Interface2 { // all methods from Interface1 and Interface2 must be coded. // ParentClass is inherited.... }

4 © 2006 Pearson Addison-Wesley. All rights reserved 9.4.4 /** 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) Comparable Interface Specifications A class that implements Comparable is used to support objects that can be compared for greater than and equal to. Note that the code for this method must be supplied by the implementing class. «interface» Comparable «query» + int compareTo(Object z)

5 © 2006 Pearson Addison-Wesley. All rights reserved 9.4.5 Creating an interface is much like creating an abstract class. Example Interfaces use the work interface in place of class Consider the need to store and manipulate natural numbers (integers greater or equal to 1) using a variety of representations. All interface methods have “;” in place of a body. Interfaces are stored in.java files with their name.

6 © 2006 Pearson Addison-Wesley. All rights reserved 9.4.6 «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() NaturalNumber # SomeNumericType thisValue «constructor» + NaturalNumber() «query» + int toInt() + String toString() «update» + void incrementByOne() RomanNumber «constructor» + RomanNumber() «query» + String toString() «update» + void setFromRoman( String s )

7 © 2006 Pearson Addison-Wesley. All rights reserved 9.4.7 public interface NaturalNumberInterface { public int toInt(); public String toString(); public void set( int j); public abstract void incrementByOne(); } 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++; } 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++; }

8 © 2006 Pearson Addison-Wesley. All rights reserved 9.4.8 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; }... } 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; }... }

9 © 2006 Pearson Addison-Wesley. All rights reserved 9.4.9 Inheritance syntax Abstract ClassInterface Mark methods as “abstract”? Non-deferred methods possible? Instance variables possible? Subclasses must code deferred? Can be inherited multiply? Yes no extends implements Yes no no Yes Yes Yes, if initialized


Download ppt "Lecture 9.4 Java Interfaces. © 2006 Pearson Addison-Wesley. All rights reserved 9.4.2 Java does not support multiple inheritance. Interface Characteristics..."

Similar presentations


Ads by Google