lec5 - COMP201 Java Programming Topic 5: Interfaces and...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP201 Java Programming Topic 5: Interfaces and Inner Classes Readings: Chapter 6
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 5 / Slide 2 Interfaces/Outline Introduction and An Example Basic issues Defining interfaces Using interfaces Interfaces and abstract classes More advanced issues Interfaces and callbacks The Cloneable Interface
Background image of page 2
COMP201 Topic 5 / Slide 3 Introduction to Interfaces An interface is not a class but a set of requirements for classes that want to conform to the interface. An interface consists of a collection constants and a collection of method with certain signatures (names plus parameter lists, function prototypes, function headers). Interfaces never have instance fields and the methods in the interface are never implemented. Interface provides a way to say that a class must implement certain methods ( Ensure objects to have certain behaviors ). It establishes a “protocol” between classes. So the supplier of some service states: “If your class conforms to a particular interface, then I’ll perform the service”. Preferred method for implementing callback functions
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 5 / Slide 4 An Example java.util.Arrays has method static void sort(Object[] a) Task: Use the method to sort an array of Employees Problem: the method requires that the objects to be sorted must belong to classes that implements the Comparable interface ( java.lang ): public interface Comparable { int compareTo( Object b); }
Background image of page 4
COMP201 Topic 5 / Slide 5 An Example Modify Employee so that it implement s the comparable interface: class Employee implements Comparable { … public int compareTo ( Object otherObject) { Employee other = (Employee)otherObject; if (salary < other.salary) return -1; if (salary > other.salary) return 1; return 0; } }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 5 / Slide 6 An Example Now we can use the sort method of Arrays to sort an array of Employees: Employee[] staff = new Employee[3]; staff[0] = new Employee("Tony Tester", 38000); staff[1] = new Employee("Harry Hacker", 35000); staff[2] = new Employee("Carl Cracker", 75000); Arrays.sort(staff); // print out information about all Employee objects for ( int i = 0; i < staff.length; i++) { Employee e = staff[i]; System.out.println("name=" + e.getName() + ",salary=" + e.getSalary()); } // EmployeeSortTest.java
Background image of page 6
COMP201 Topic 5 / Slide 7 Defining Interfaces General skeleton: public interface NameofInterface extends AnotherInterface { method1; method2; constant1; constant2; … } All methods are abstract by default, no need for modifier abstract All fields are constants by default, no need for modifier static final All methods and constants have public access by default, no need for modifier public .
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 5 / Slide 8 Defining Interfaces An example public interface Moveable { void move( doube x, double y); } public interface Powered extends Moveable { String powerSource(); int SPEED_LIMIT = 95; }
Background image of page 8
COMP201 Topic 5 / Slide 9 Using Interfaces
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 45

lec5 - COMP201 Java Programming Topic 5: Interfaces and...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online