Download presentation
Presentation is loading. Please wait.
Published byMervyn Caldwell Modified over 9 years ago
1
Chapter 6 Interfaces
2
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
3
Generic Array List Dynamically add/remove array elements No over-allocation No manual reallocation
4
Assignment 4 Using ArrayList New Solution – See Eclipse
5
Methods With Variable Number of Parameters
6
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; } Si mply 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.
7
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
8
Interface Syntax modifier interface InterfaceName { /* constant declarations */ /* abstract method signatures */ } Public interface Edible { /** describe how to eat */ public abstract String howToEat(); }
9
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
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
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()); } }
12
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.
13
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
14
Employee Class Sorting see sample eclipse code
15
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
16
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
17
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
18
Assignment 5 Due Jan 8th
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.