Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Interfaces A Java interface is a collection
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
Java Software Solutions
Chapter 8 Inheritance Part 2. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Inheritance Inheritance Reserved word protected Reserved word super
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”
Interfaces A Java interface is a collection of constants and abstract methods with a name that looks like a class name, i.e. first letter is capitalized.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Multiple Choice Solutions True/False a c b e d   T F.
Object Oriented Design and UML
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Polymorphi sm. 2 Abstract Classes Java allows abstract classes – use the modifier abstract on a class header to declare an abstract class abstract class.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 6: Object-Oriented Design.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Designing Classes Chapter 3. Contents Encapsulation Specifying Methods Java Interfaces – Writing an Interface – Implementing an Interface – An Interface.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Chapter VII: Arrays.
Chapter 7 Object-Oriented Design
Chapter 5: Enhancing Classes
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
ADT’s, Collections/Generics and Iterators
Chapter 5: Enhancing Classes
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Lecture 16 - Interfaces Professor Adams.
Chapter 5: Enhancing Classes
Overriding Methods & Class Hierarchies
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Chapter 8 Inheritance.
2009 Test Key.
Object-Oriented Design Part 2
Presentation transcript:

Interfaces

In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do with it? The implementation details are irrelevant to using the class  you don’t have to know how things are done inside the class

Interfaces A Java interface is a collection of abstract methods and constants An abstract method is a method header without a method body An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, usually it is left off An interface is used to establish a set of methods that a class will implement

Interfaces public interface Doable { public void doThis(); public int doThat(); public void doThis2 (float value, char ch); public boolean doTheOther (int num); } interface is a reserved word None of the methods in an interface are given a definition (body) A semicolon immediately follows each method header

Interfaces An interface cannot be instantiated Methods in an interface have public visibility by default A class formally implements an interface by:  stating so in the class header  providing implementations for each abstract method in the interface If a class asserts that it implements an interface, it must define all methods in the interface

Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public void doThat () { // whatever } // etc. } implements is a reserved word Each method listed in Doable is given a definition

Interfaces A class that implements an interface can implement other methods as well In addition to (or instead of) abstract methods, an interface can contain constants When a class implements an interface, it gains access to all its constants

Comparison with Inheritance Interfaces don’t define any method actions… you need to fill in all the details It essentially just gives a basic collection of method names Not a strict hierarchy Important: can implement several Interfaces Can use this to “fake” multiple inheritance

Interfaces A class can implement multiple interfaces The class must implement all methods in all interfaces listed in the header class ManyThings implements interface1, interface2 { // all methods of both interfaces }

Example:The Comparable Interface Any class can implement Comparable to provide a mechanism for comparing objects of that type Specifically, implementing Comparable means that you need a method CompareTo if (obj1.compareTo(obj2) < 0) System.out.println ("obj1 is less than obj2");

The Comparable Interface It's up to the programmer to determine what makes one object less than another For example, you may define the compareTo method of an Employee class to order employees by name (alphabetically) or by employee number The implementation of the method can be as straightforward or as complex as needed for the situation

Requiring Interfaces Interface names can be used like class names in the parameters passed to a method public boolean isLess(Comparable a, Comparable b) { return a.compareTo(b) < 0; } Any class that “implements Comparable ” can be used for the arguments to this method

Interfaces in UML Interfaces are easy to spot in class diagrams

Interfaces You could write a class that implements certain methods (such as compareTo ) without formally implementing the interface ( Comparable ) However, formally establishing the relationship between a class and an interface allows Java to deal with an object in certain ways Interfaces are a key aspect of object-oriented design in Java

Built-in Interfaces The Java standard library includes lots more built-in interfaces  they are listed in the API with the classes Examples:  Clonable – implements a clone() method  Formattable – can be formatted with printf

The Iterator Interface As we discussed in Chapter 5, an iterator is an object that provides a means of processing a collection of objects one at a time An iterator is created formally by implementing the Iterator interface, which contains three methods The hasNext method returns a boolean result – true if there are items left to process The next method returns the next object in the iteration The remove method removes the object most recently returned by the next method

The Iterator Interface By implementing the Iterator interface, a class formally establishes that objects of that type are iterators The programmer must decide how best to implement the iterator functions Once established, the for-each version of the for loop can be used to process the items in the iterator

Collections Collection is a general interface for any type that can store multiple values Any object c that implements Collections has these methods  c.add(e)  c.remove(e)  c.size()

Collection Sub-Interfaces Interfaces that are derived from Collection Set : unordered, can’t add the same object twice List : ordered, adds new methods  get(i): get the i th element  set(i,e): set the i th element to e

Collection Implementations Also in the standard library: many good implementations of these interfaces List: ArrayList, Stack, LinkedList Sets: HashSet, TreeSet Each implementation has some differences… suitable for particular problems  e.g. additional methods, different type restrictions, etc.

Example: Pairs A class to represent a pair (x,y) of values Both values represented with Double class Pair { Double x, y; public Pair(double x, double y) { this.x = new Double(x); this.y = new Double(y); }

Example: Pairs Want to be able to compare.. class Pair implements Comparable { …. public int compareTo(Pair other) { if(this.x.equals(other.x)) return this.y.compareTo(other.y); else return this.x.compareTo(other.x); }

Implementing versus Inheriting Implementing an Interface is very similar to inheriting a class class MyClass implements MyInterface {…}  Takes everything from MyInterface and puts it in MyClass  Except all the methods must be implemented here  No previous implementations to fall back on

Interfaces vs. Abstract Classes Similarities  neither can be instantiated  both can be used as the starting point for a class Differences  A class can contain implementations of methods  A class can implement many interfaces, but only one class

Comparison In order of “abstractness”:  Interface no method implementations can’t be instantiated  Abstract class some method implementations can’t be instantiated  Non-abstract class all methods implemented can be instantiated