1 Interfaces Sec. 12.1 contains this material. Corresponding lectures on ProgramLive CD is an alternative way to learn the material. Top finalists from.

Slides:



Advertisements
Similar presentations
A magazine ran a Dilbert quotes contest. These are actual quotes from managers out there. Real-Life Dilbert Quotes.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Interfaces CSC 171 FALL 2004 LECTURE 14. Project 1 review public class Rational { private int numerator, denominator; public Rational(int numerator, int.
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.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture22.
Generic programming in Java
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
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 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
Introduction to Searching and Sorting
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
1 Listening to events on GUIs Sec contains this material. Corresponding lectures on ProgramLive CD is a better way to learn the material. Top finalists.
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.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
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.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Generic abstraction  Genericity  Generic classes  Generic procedures Programming Languages 3 © 2012 David A Watt, University of Glasgow.
1 CS100J 2 December 2008 Applications and Applets Read Chapter 16 of the text We also look at html, since we need it to use applets. Top finalists from.
Best Practices. Contents Bad Practices Good Practices.
1 Interfaces Reading for today: Sec and corresponding ProgramLive material. Also, compare with previous discussions of abstract classes (Sec. 4.7).
1 CS Nov 2011 Applications and Applets Read Chapter 16 of the text We also look at html, since we need it to use applets. Top finalists from a real-life.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
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.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Interfaces and Inner Classes
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 “A picture is.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
More constructors 1. Constructors  recall that a public constructor is what a client uses to create an object  the purpose of a constructor is to initialize.
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.
Lecture 5:Interfaces and Abstract Classes
Object-Oriented Concepts
Final exam: Period B: Thursday, 13 May, 9:00-11:30AM, Barton East
We also look at html, since we need it to use applets.
CS/ENGRD 2110 Spring 2017 Lecture 7: Interfaces and Abstract Classes
We also look at html, since we need it to use applets.
Minimal Spanning Trees
CS/ENGRD 2110 fall 2017 Lecture 7: Interfaces and Abstract Classes
null, true, and false are also reserved.
An overview of Java, Data types and variables
Interfaces – handling very “general” operations cleanly (April 21)
Generic programming in Java
COMPUTER 2430 Object Oriented Programming and Data Structures I
CS/ENGRD 2110 Spring 2019 Lecture 7: Interfaces and Abstract Classes
TCSS 143, Autumn 2004 Lecture Notes
Chapter 13 Abstract Classes and Interfaces Part 01
Presentation transcript:

1 Interfaces Sec contains this material. Corresponding lectures on ProgramLive CD is an alternative way to learn the material. Top finalists from a real-life “Dilbert quotes contest” As of tomorrow, employees will be able to access the building only using individual security cards. Pictures will be taken next Wednesday and employ- ees will receive their cards in two weeks." (Fred Dales, Microsoft) I need an exact list of specific unknown problems we might encounter. (Lykes Lines Shipping) is not to be used to pass on information or data. It should be used only for company business. (Accounting manager, Electric Boat Company) This project is so important, we can't let things that are more important interfere with it. (Advertising/Marketing manager, United Parcel Service) Doing it right is no excuse for not meeting the schedule. (Plant manager, Delco Corporation)

2 Interface: used to enforce that a class overrides certain methods /** Komparable provides a method for comparing two objects */ public interface Komparable { /** = 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. */ public int kompareTo(Object c); } Body replaced by ; Every class that “implements” Komparable must override kompareTo

3 public class Date implements Komparable { public int year; /** year. */ public int month; /** month, in the range */ public int day; /** The day of the month. */ /** = -1, 0, or +1 if this date comes before, is same as, or comes after c. Throw a ClassCastException if c cannot be cast to Date.*/ public int kompareTo(Object c) { if (!(c instanceof Date)) throw new ClassCastException("argument is not a Date"); Date d= (Date) c; if (year == d.year && month == d.month && day == d.day) return 0; if ( year < d.year || (year == d.year && month < d.month) || (year == d.year && month == d.month && day < d.day)) return -1; return 1; } class will contain other methods

4 You can use 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].kompareTo(b[i]) < -1) { Komparable temp= b[i]; b[i]= b[j]; b[j]= temp; } Komparable Use Komparable.kompareTo to compare 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 Komparable. polygon polygamy polychord polydemic polytechnic

5 Interface java.util.Comparable /** Comparable requires method compareTo and method equals*/ 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 …

6 Form of an interface declaration /** comment*/ public interface { /** method spec for function*/ int compareTo(…); /** method spec for procedure */ void compareTo(…); /** 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

7 A class can implement several interfaces /** comment*/ public class C implements Inter1, Inter2, Inter3{ … } The class must override all methods declared in interfaces Inter1, Inter2, and Inter3.