1 Interfaces Reading for today: Sec. 12.1 and corresponding ProgramLive material. Also, compare with previous discussions of abstract classes (Sec. 4.7).

Slides:



Advertisements
Similar presentations
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Advertisements

DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
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.
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.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
CS102 Data Types in Java CS 102 Java’s Central Casting.
CPSC150 Interfaces Chapter CPSC150 Inheritance Review No different than any other class. Has no access to or information about subclasses class.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Abstract Classes and Interfaces
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
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.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
Polymorphism & Interfaces
1 Lecture 07 Interfaces and related issues Reading for these lectures: Weiss, Section 4.4 (The interface), p ProgramLive, Section 12.1 In User Interface.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
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.
Interfaces & Polymorphism part 2:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
Programming in Java CSCI-2220 Object Oriented Programming.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Object Oriented Programming.  Interface  Event Handling.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
1 Interfaces Sec contains this material. Corresponding lectures on ProgramLive CD is an alternative way to learn the material. Top finalists from.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Interfaces and Inner Classes
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
1 Interface Design. 2 concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Abstract Classes.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Final exam: Period B: Thursday, 13 May, 9:00-11:30AM, Barton East
Chapter 11 Abstract Classes and Interfaces
Inheritance, Polymorphism, and Interfaces
null, true, and false are also reserved.
Interfaces.
Abstract classes, Interfaces
Interfaces – handling very “general” operations cleanly (April 21)
Chapter 14 Abstract Classes and Interfaces
Presentation transcript:

1 Interfaces Reading for today: Sec and corresponding ProgramLive material. Also, compare with previous discussions of abstract classes (Sec. 4.7). Final exam: Period B: Thursday, 13 May, 9:00-11:30AM, Barton East Contact ASAP regarding conflicts. Explain the nature of the conflict in detail (e.g. which other exam it conflicts

Can extend only one class public class C extends C1, C2 { … } 2 public class C1 { public int m() { return 2; } public int p() { return …; } public class C2 { public int m() { return 3; } public int q() { return …; } if we allow multiple inheritance, which m is used?

Can extend only one class public class C extends C1, C2 { … } 3 public abstract class C1 { public abstract int m(); public abstract int p(); } public abstract class C2 { public abstract int m(); public abstract int q(); } But this would be OK, because the bodies of the methods are not given! Nevertheless, not allowed

Use an “interface” public class C implements C1, C2 { … } 4 public interface C1 { int m(); int p(); } public interface C2 { int m(); int q(); } Methods declared in an interface must be abstract! No need for “abstract”, automatically public

Example: Listening to a mouse click (or other object- appropriate action) 5 Defined in package java.awt.event public interface ActionListener extends Eventlistener { /** Called when action occurs. */ public void actionPerformed(ActionEvent e); } /** An instance has two buttons. Exactly one is always enabled. */ public class ButtonDemo1 extends JFrame implements ActionListener { /** Process a click of a button */ public void actionPerformed (ActionEvent e) { boolean b= eastB.isEnabled(); eastB.setEnabled(!b); westB.setEnabled(b); }

6 A use of interfaces: remove need for duplicate code for special cases of a general approach Example: sorting is general, but the notion of “<“ may change: Recommender systems should sort products (movies, songs …) by quality or by how much you, personally, would like them. Travel sites should sort flights by price, departure, etc. Don’t want to write many sort procedures: public void sort(int[] arr) {…} public void sort(double[] arr) {…} public void sort(Movie[] arr) {…} public void sort(Flight[] arr) {…} Use an interface to enforce the existence of a method that does the comparison of two objects

7 Interface java.util.Comparable /** Comparable requires method compareTo*/ public interface Comparable { /** = a negative integer if this object < c, = 0 if this object = c, = a positive integer if this object > c. Throw a ClassCastException if c cannot be cast to the class of this object. */ int compareTo(Object c); } Classes that implement Comparable Boolean Byte Double Integer … String BigDecimal BigInteger Calendar Time Timestamp … abstract method: body replaced by ; Every class that implements Comparable must override compareTo(Object).

8 Using an interface as a type /** Swap b[i] and b[j] to put larger in b[j] */ public static void swap( int [] b, int i, int j) { } if (b[j].compareTo(b[i]) < 0) { Comparable temp= b[i]; b[i]= b[j]; b[j]= temp; } Comparable Polymorphism: the quality or state of existing in or assuming different forms This parametric polymorphism allows us to use swap to do its job on any array whose elements implement Comparable.

9 /** An instance is a movie and what critics thought of it. */ public class Movie implements Comparable { public String name; /** movie name. */ private int[10] ratings; /**ratings from 10 critics. */ private int final NOTSEEN= 0; /** rating if not seen by given critic */ /** Actual ratings are: 1, 2, 3, 4, or 5 */ /** = -1, 0, or +1 if this Movie’s name comes alphabetically before, at, or after c. Throw a ClassCastException if c cannot be cast to Movie.*/ public int compareTo(Object c) { if (!(c instanceof Movie)) throw new ClassCastException("argument is not a Movie"); // Note: String implements Comparable Movie cm= (Movie) c; return this.name.compareTo(cm.name); } class will contain other methods

10 Declaring your own interfaces /** comment*/ public interface { /** method spec for function*/ int compareTo(…); /** method spec for procedure */ void doSomething(…); /** explanation of constant x*/ int x= 7; } Every field is implicitly public, static, and final. You can put these modifiers on them if you wish. Methods are implicitly public. You can put the modifier on if you wish. Use “;” instead of a body

11 A class can implement several interfaces /** comment*/ public class C implements Inter1, Inter2, Inter3{ … } Example: a recommendation system that returns all movies that satisfy some minimum similarity to one of your favorites. Need to sort and to measure similarity (a general task worthy of an interface). The class must override all methods declared in interfaces Inter1, Inter2, and Inter3.