Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCSS 143, Autumn 2004 Lecture Notes

Similar presentations


Presentation on theme: "TCSS 143, Autumn 2004 Lecture Notes"— Presentation transcript:

1 TCSS 143, Autumn 2004 Lecture Notes
Java Interfaces

2 Java interfaces interface: a set of declared (but not defined) methods and fields that a class can promise to implement declared: given a name and header defined: declared, plus given a body provides a way to treat many classes as though they were the same examples: java.util.Iterator, java.awt.event.ActionListener, java.lang.Comparable

3 Recall: compareTo public int compareTo(Object o)
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. In summary: negative integer: this object is less than o zero: this object is equal to o positive integer: this object is greater than o

4 Comparable interface actually, a class with the compareTo method should implement the java.lang.Comparable interface Comparable declares one method: public int compareTo(Object o) by declaring that your class implements Comparable, you allow many classes in Java to compare your objects intelligently

5 Implementing an interface
write implements InterfaceName on class's header define all of the methods in the interface in your class a class may implement many interfaces; separate their names by commas public class ImaginaryNumber implements Comparable, Serializable, Observer { ... }

6 compareTo example public class Person implements Comparable {
private String myName; private int myPhoneNumber; /* Compares Person objects by name, breaking ties by phone number. */ public int compareTo(Object o) { Person p = (Person)o; int nameCmp = myName.compareTo(p.getName()); if (nameCmp != 0) return nameCmp; else return myPhoneNumber - p.getPhoneNumber(); } }

7 Polymorphism interfaces are useful because they provide a way to achieve polymorphism in Java polymorphism: the ability to use identical syntax on different data types, causing possibly different underlying behavior to execute objects that agree to behave the same on the outside, but might be very different on the inside examples? encapsulation, polymorphism, and reusability are the foundations of object-oriented programming telephones vs. TV remotes; cars

8 Interface reference variables
it is illegal to construct an object of an interface type Comparable c1 = new Comparable(); however, it is legal to declare a variable of an interface's type Comparable c1 = "hello"; Comparable c2 = new Fraction(2, 3); observation: the type of a reference variable need not necessarily match the type of the object that it refers to

9 Interface reference variables
a variable of an interface type can be assigned to refer to any object that implements that interface using the interface variable, one may call any of the interface's methods (or methods that exist in class Object, such as equals and toString) on the object it is illegal to call methods of the object that aren't in the interface (or class Object), through the interface variable to regain the ability to call the other methods, you can cast the reference variable to the appropriate type

10 Interface reference variables
Comparable c1 = "hello"; System.out.println(c1.compareTo(c1)); System.out.println(c1.toString()); System.out.println(((String)c1).substring(1, 4)); it is legal to call these on c1: compareTo equals, toString any String method on ((String)c1) it is illegal to call these on c1: any String method on c1 itself, without casting

11 Variables of type Object
just as it is legal to declare variables of an interface type, you can also have variables of type Object that can refer to any object using a variable of type Object, one may only call methods from the Object class! to use any other methods, do a type-cast Object o = "hello"; Object o2 = new BankAccount("Marty", 1.0); Why would we ever want this kind of variable?

12 Interface reference variables
practice problems: Which lines are legal? Object obj = "hello"; Comparable cmp = "hello"; System.out.println(cmp.compareTo("goodbye")); System.out.println(obj.indexOf('e')); System.out.println(cmp + ", world"); System.out.println(cmp.toUpperCase()); System.out.println(((String)cmp).trim()); System.out.println(obj.equals(null)); System.out.println(("" + cmp).toLowerCase()); System.out.println(obj.toString().indexOf('h')); legal illegal

13 Class cast exceptions type-casting a reference variable from an interface type into the wrong class type causes a ClassCastException to occur at runtime Why doesn't the compiler catch this error? Comparable c = new Fraction(2, 3); System.out.print(((String)c).toLowerCase()); when the program is run: Exception in thread "main" java.lang.ClassCastException at temp.main(temp.java:4)

14 compareTo search / sorting
practice problem: Write a static method isSorted that accepts an array of Fractions as its argument, and returns true if the Fractions in the array are stored in ascending order by value, and false if not. Make the isSorted method above generalized so that it will work for an array of any type of comparable objects.

15 Declaring an interface
header: public interface InterfaceName methods written with header, semicolon ; (no body) interfaces may only have public methods no constructors no methods that exist in class Object interfaces may contain public static final constants public interface Vehicle { public int getWeight(); public boolean setSpeed(double newSpeed); public void go(City place, String message); }

16 References Koffman, Chapter 1, pp


Download ppt "TCSS 143, Autumn 2004 Lecture Notes"

Similar presentations


Ads by Google