Top Banner
Chapter 6 Interfaces
18

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)

Jan 17, 2016

Download

Documents

Mervyn Caldwell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 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)

Chapter 6

Interfaces

Page 2: 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)

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

Page 3: 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)

Generic Array List• Dynamically add/remove array elements • No over-allocation• No manual reallocation

Page 4: 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)

Assignment 4 Using ArrayList

• New Solution – See Eclipse

Page 5: 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)

Methods With Variable Number of Parameters

Page 6: 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)

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.

Page 7: 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)

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

Page 8: 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)

Interface Syntax

modifier interface InterfaceName {/* constant declarations *//* abstract method signatures */

}Public interface Edible {

/** describe how to eat */public abstract String howToEat();

}

Page 9: 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)

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"; }}

Page 10: 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)

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

Page 11: 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)

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()); }}

Page 12: 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)

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.

Page 13: 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)

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

Page 14: 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)

Employee Class Sortingsee sample eclipse code

Page 15: 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)

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

Page 16: 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)

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

Page 17: 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)

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

Page 18: 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)

Assignment 5

• Due Jan 8th