Final exam: Period B: Thursday, 13 May, 9:00-11:30AM, Barton East

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.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
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.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Abstract Classes and Interfaces
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.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
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()
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.
1 Interfaces Reading for today: Sec and corresponding ProgramLive material. Also, compare with previous discussions of abstract classes (Sec. 4.7).
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.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
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.
Interfaces and Inner Classes
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
SCJP 5, 1/7 Declarations, Initialization and Scoping
1 1 Chapter 14 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code.
Lecture 5:Interfaces and Abstract Classes
Lecture 15.1 Event Delegation.
Modular Event Handling
CSC 205 Programming II Lecture 5 AWT - I.
CompSci 230 S Programming Techniques
Chapter 11 Abstract Classes and Interfaces
Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
Methods Attributes Method Modifiers ‘static’
Chapter 10 Thinking in Objects
Interfaces and Inheritance
Chapter 13 Abstract Classes and Interfaces
Inheritance, Polymorphism, and Interfaces
Lecture 4: Interface Design
CMSC 202 Interfaces.
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Java Programming Language
Week 6 Object-Oriented Programming (2): Polymorphism
Interfaces.
Abstract classes, Interfaces
Interfaces – handling very “general” operations cleanly (April 21)
Lecture 16 - Interfaces Professor Adams.
Chapter 12 Abstract Classes and Interfaces
CS/ENGRD 2110 FALL 2016 Lecture 7: Interfaces and Abstract Classes
Generic programming in Java
CSC 113: Computer programming II
CMSC 202 Interfaces.
Chapter 14 Abstract Classes and Interfaces
FINAL EXAM Final Exam Wednesday, Dec 14: 8: :00 AM (Frny G140)
Review of Previous Lesson
Creating and Modifying Text part 3
Chapter 13 Abstract Classes and Interfaces Part 01
Chapter 11 Inheritance and Encapsulation and Polymorphism
CMSC 202 Interfaces.
FINAL EXAM Final Exam Tuesday, May 3: 1:00 - 3:00 PM (Phys 112)
Presentation transcript:

Final exam: Period B: Thursday, 13 May, 9:00-11:30AM, Barton East Interfaces Reading for today: Sec. 12.1 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 mwitlox@cs.cornell.edu ASAP regarding conflicts. Explain the nature of the conflict in detail (e.g. which other exam it conflicts with).

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

Can extend only one class public class C extends C1, C2 { … } public abstract class C1 { public abstract int m(); public abstract int p(); } public abstract class C2 { 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 { … } public interface C1 { int m(); int p(); } public interface C2 { int q(); Methods declared in an interface must be abstract! No need for “abstract”, automatically public

Example: sorting is general, but the notion of “<“ may change: 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

Interface java.util.Comparable Classes that implement Comparable Boolean Byte Double Integer … String BigDecimal BigInteger Calendar Time Timestamp /** 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); } abstract method: body replaced by ; Every class that implements Comparable must override compareTo(Object).

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) { } Comparable if (b[j].compareTo(b[i]) < 0) { Comparable temp= b[i]; b[i]= b[j]; b[j]= temp; } 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.

class will contain other methods /** 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

/** An instance has two buttons. Exactly one is always enabled. */ Another example: Listening to a mouse click (or other object-appropriate action) 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); }

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

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.