Chapter 6 Interfaces
Jan 17, 2016
Chapter 6
Interfaces
Class Status
• CU will be close for winter break from Dec. 22. till Jan.2
• We have 3 classes left after tonight (Jan 8,15, and 22)
• Books still aren’t in yet. • Start looking at Chapter 11 next
Generic Array List• Dynamically add/remove array elements • No over-allocation• No manual reallocation
Assignment 4 Using ArrayList
• New Solution – See Eclipse
Methods With Variable Number of Parameters
Varargs Method’s Cont
public static double max(double... values){double largest = Double.MIN_VALUE;for (double v : values) if (v > largest) largest = v;return largest;}
• Simply call the function like this:double m = max(3.1, 40.4, -5);• The compiler passes a new double[] { 3.1, 40.4, -5 } to
the max function.
Welcome to Interfaces
• What is a class?– Defines methods with inputs and outputs– Describes HOW to do something
• Compare and contrast: interfaces– Also defines the inputs and outputs of methods– is not a class but a set of requirements for classes that want to
conform to the interface• Describes WHAT a class should do
• In other words - Interfaces are a way to specify common behavior for objects
• Classes can implement one or more interfaces
Interface Syntax
modifier interface InterfaceName {/* constant declarations *//* abstract method signatures */
}Public interface Edible {
/** describe how to eat */public abstract String howToEat();
}
9
Creating Custom Interfaces
public interface Edible { /** Describe how to eat */ public String howToEat();}
class Animal {}
class Chicken extends Animal implements Edible { public String howToEat() { return "Fry it"; }}
class Tiger extends Animal {}
class abstract Fruit implements Edible {}
class Apple extends Fruit { public String howToEat() { return "Make apple cider"; }}
class Orange extends Fruit { public String howToEat() { return "Make orange juice"; }}
10
class Chicken extends Animal implements Edible, Comparable { int weight; public Chicken(int weight) { this.weight = weight; } public String howToEat() { return "Fry it"; } public int compareTo(Object o) { return weight – ((Chicken)o).weight; }}
Implements Multiple Interfaces
11
Creating Custom Interfaces, cont.
public interface Edible { /** Describe how to eat */ public String howToEat();}
public class TestEdible { public static void main(String[] args) { Object[] objects = {new Tiger(), new Chicken(), new Apple()}; for (int i = 0; i < objects.length; i++) showObject(objects[i]); }
public static void showObject(Object object) { if (object instanceof Edible) System.out.println(((Edible)object).howToEat()); }}
Example – Arrays.sort()
• Now suppose we want to use the sort method of the Arrays class to sort an array of Employee objects. Then the Employee class must implement the Comparable interface.
• To make a class implement an interface, you carry out two steps:1. You declare that your class intends to implement the given interface.2. You supply definitions for all methods in the interface.
Example – Arrays sort()
• The sort() method of the Arrays class can sort objects of any arbitrary class
• To be sortable, a class must implement the Comparable interface
Employee Class Sortingsee sample eclipse code
Properties of Interfaces
An interface variable must refer to an object of a class that implements the interface:x = new Employee(. . .); // OK provided Employee implements Comparable
Cannot instantiate interface Objects i.e. can’t call new
Object Cloning
• Copying– Makes a reference to the same object– Contents of original and copy are always the same
• Cloning– Makes a true copy– Contents of original and clone can diverge
• The clone method is a protected method of Object, which means that your code cannot simply call it.
• Only a given class can clone instances of that class
Difference between copying and cloning
Employee original = new Employee("John Public", 50000);Employee copy = original;copy.raiseSalary(10); // oops--also changed original
Employee copy = original.clone();copy.raiseSalary(10); // OK--original unchanged
Assignment 5
• Due Jan 8th