1 Role of Interfaces in Large-Scale Software l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data elements.

Slides:



Advertisements
Similar presentations
12 Copyright © 2005, Oracle. All rights reserved. Structuring Code Using Abstract Classes and Interfaces.
Advertisements

11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Computer Science 209 Software Development Equality and Comparisons.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
Comparable and Comparator Nuts and Bolts. 2 Nuts and bolts Four methods underlie many of Java’s important Collection types: equals, compare and compareTo,
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
1 Lecture 08(API)Lecture 11 Java API and Interfaces l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
Introduction to Searching and Sorting
CS 2511 Fall Features of Object Oriented Technology  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance.
Comparable and Comparator. 2 Comparing our own objects The Object class provides public boolean equals(Object obj) and public int hashCode() methods –For.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Set, TreeSet, TreeMap, Comparable, Comparator. Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
1 Interfaces Reading for today: Sec and corresponding ProgramLive material. Also, compare with previous discussions of abstract classes (Sec. 4.7).
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Comparable and Comparator Nuts and Bolts. Sets A set is a collection in which all elements are unique—an element is either in the set, or it isn’t In.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
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)
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
Interfaces and Inner Classes
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Java Collection Classes Com379PT
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
Motivation for Generic Programming in C++
Modern Programming Tools And Techniques-I
Final exam: Period B: Thursday, 13 May, 9:00-11:30AM, Barton East
Chapter Goals To be able to declare and use interface types
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
Interfaces and Inheritance
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
Introduction to Searching and Sorting
Comparable and Comparator
CMSC 202 Interfaces.
Interfaces.
Comparable and Comparator
Object Oriented Programming in java
Creating and Modifying Text part 3
TCSS 143, Autumn 2004 Lecture Notes
CMSC 202 Interfaces.
Arrays.
Presentation transcript:

1 Role of Interfaces in Large-Scale Software l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data elements of a class Sort: A Real-World Example l Used by a number of unrelated classes l Contains a known set of methods l Need it to sort any type of object l Comparison rules known only to the sortable object l Good code reuse

2 Role of Interfaces in Large-Scale Software Java API and Interfaces The Java API makes heavy use of interfaces since they provide a powerful tool. As an example, consider the Arrays class defined in the java.util package. This class has one method called sort(). One of this method signature is: The method sort() sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array). The Comparable represents an interface that has to be implemented by all classes aiming for using the sort() method of the Arrays class. The implementing classes have to override the following Comparator methods: int compareTo(Object o) public static void sort(Object [] a)

3 Role of Interfaces in Large-Scale Software Using Interfaces l A sort is a classic example of the use of an interface. By using good OO programming technique and interfaces, you can eliminate all of the maintenance difficulties associated with the traditional approach. l The Sortable interface specifies the methods required to make the sort work on each type of object that needs to be sorted. Each class implements the interface based on its specific sorting needs. Only the class needs to know its object comparison. l The sort code is completely isolated from the objects that implement the sort algorithm.

4 Role of Interfaces in Large-Scale Software API Sort Interface Example l Three classes and one interface involved in sorting a list of videos l The sortable interface declares one method: compare(), This method must be implemented by any class that wants to use the sort class methods l The Sort class is an abstract class that contains sortObjects( ), a method to sort an array of objects. sortObjects( ) does comparison by calling the compare ( ) method on the objects in the array. l MyApplication rpresents any application that needs to sort a list of movies Overview of the Classes public interface Sortable public abstract class Sort public class Movie implements Sortable public class MyApplication Created by the sort expert Created by the movie expert

5 Role of Interfaces in Large-Scale Software Sort Interface: Details MyApplication 1 MyApplication passes an array of movies to Sort.sortObjects() Sort Movie sortObjects() asks a movie to compare itself with another movie 2 The movie returns the result of the comparison 4 sortObjects() returns the sorted list 3

6 Role of Interfaces in Large-Scale Software The Sortable Interface public interface Sortable { // compare(): Compare this object to another object // Returns: // 0 if this object is equal to obj2 // a value < 0 if this object < obj2 // a value > 0 if this object > obj2 int compare(Object obj2); } The Sortable interface specifies all of the methods and constants required for a class to be sortable. In the example, the only method is compare( ). Any class that implements Sortable must provide a compare() method that accepts an Object argument and returns an int.

7 Role of Interfaces in Large-Scale Software The Sort Class The sort class contains the sortObjects() method, which sorts an array of Sortable objects, sortObjects() accepts an array of Sortable objects as its argument. It is legal syntax to specify an interface type for a method’s arugument; in this case it ensures that the method will only be asked to sort objects that implement the Sortable interface. When sortObjects() needs to compare two items in the array, it calls compare() on one of the items, passing the other item as the argument public abstract class Sort { public static void sortObjects(Sortable[] items) { // Step through the array comparing and swapping; // do this length-1 times for (int i = 1; i < items.length; i++) { for (int j = 0; j < items.length - 1; j++) { if (items[j].compare(items[j+1]) > 0) { Sortable tempitem = items[j+1]; items[j+1] = items[j]; items[j] = tempitem; } } } } } Holds sortobjects( )

8 Role of Interfaces in Large-Scale Software The Movie Class The Movie class implements the Sortable interface. If it wants to call Sort.sortOBJECTS() it must implement the Sortable interface, and if it implements the Sortable interface it must implement the compare( ) method. The compare( ) method takes an Object as an argument and compares it with the object on which it was called.we use the String compareTo( ) method to compare the two title strings. public class Movie extends InventoryItem implements Sortable { String title; public int compare(Object movie2) { String title1 = this.title; String title2 = ((Movie)movie2).getTitle(); return(title1.compareTo(title2)); } // End of compare() method } // End of class Movie implements Sortable

9 Role of Interfaces in Large-Scale Software Using Sort Class To use the sort, you simply call Sort.sortObjects(Sortable[]) from your application, passing the array of objects you want sorted. Each object that you want to sort must implement the Sortable interface and provide the required compare( ) method. class myApplication { Movie[] movielist; … // build the array of Movie Sort.sortObjects(movielist); } Call Sort.sortObjects(Sortable []) with an array of Movie as the argument