INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

 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).
Big Ideas behind Inheritance. Can you think of some possible examples of inheritance hierarchies?
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.
Chapter 11 – Interfaces and Polymorphism. Chapter Goals Learn about interfaces Learn about interfaces Convert between class and interface references Convert.
Interfaces and polymorphism Chapter 9. Interfaces  Used to express operations common to more than one purpose.  Example: You want to find the maximum.
Interfaces and Polymorphism Lesson - 8. Objectives Interfaces Supertype and subtype references Polymorphism Inner classes.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Nine: Interfaces and Polymorphism.
Computer Science A 9: 3/11. Inheritance Today: Inheritance (JC – CCJ ) I have to leave at 11am (but you can stay)
What is an Interface?? An interface is a ‘container’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Interfaces and Polymorphism.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Inheritance Inheritance Reserved word protected Reserved word super
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,
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Abstract Classes and Interfaces
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Java Implementation Software Construction Lecture 6.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Writing JavaDocs Mimi Opkins CECS 274 Copyright (c) Pearson All rights reserved.
What is inheritance? It is the ability to create a new class from an existing class.
1 Documenting with Javadoc. 2 Motivation  Why document programs? To make it easy to understand, e.g., for reuse and maintenance  What to document? Interface:
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Interfaces and Polymorphism 9.1 Using Interfaces for Code Reuse Defining an Interface implementing an interface 9.2 Converting between Class.
What is an Interface?? An interface is a ‘class’ which contains ONLY abstract methods. public interface Capitalizable { public abstract String outCaps()
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
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.
Documentation javadoc. Documentation not a programmer's first love lives in a separate file somewhere usually a deliverable on the schedule often not.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
CHAPTER 9 INTERFACES AND POLYMORPHISM Chapter Goals: –To learn about interfaces –To be able to convert between supertype and subtype references –To understand.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CHAPTER 9 INTERFACES AND POLYMORPHISM. CHAPTER GOALS To learn about interfaces To be able to convert between supertype and subtype references To understand.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
Drew University1 Interfaces and Polymorphism 9.1 Developing Reusable Solutions 9.2 Converting between Types 9.3 Polymorphism Common Error 9.1 Advanced.
1 Documenting with Javadoc CS 3331 Section and Appendix B of [Jia03] How to Write Doc Comments for the Javadoc TM Tool available from
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 12 – Object-Oriented Design
Chapter Goals To be able to declare and use interface types
Inheritance and Polymorphism
Lecture Notes – Interface and Polymorphism (Ch 9-10)
Interfaces and Polymorphism
Object Oriented Programming in Java
Chapter 11 – Interfaces and Polymorphism
Interfaces and Inheritance
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Java Programming Language
Chapter 9 Carrano Chapter 10 Small Java
Presentation transcript:

INTERFACES More OO Concepts

Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism

Interfaces What functions does an alarm clock have?

What type of data can be sorted?

How can we sort? JimBarryDoug SORT CompareTo Jim.compareTo(Barry) >0 Barry.compareTo(Doug) <0 Jim.compareTo(Doug) >0 (may do other compares, depends on sort algorithm) JimBarryDoug

How can we sort? Jim 3.95 Barry 3.98 Doug 3.02 SORT CompareTo Jim.compareTo(Barry) <0 Barry.compareTo(Doug) >0 Jim.compareTo(Doug) >0 Doug 3.02 Jim 3.95 Barry 3.98 Interface: Comparable Requires: public int compareTo(Object) - old style public int compareTo (ObjType)

In Code public class Student implements Comparable { private String name; private double gpa; public Student(String name, double gpa) { super(); this.name = name; this.gpa = gpa; } public int compareTo(Student other) { //return name.compareTo(other.name); if (gpa < other.gpa) return -1; else if (gpa > other.gpa) return 1; else return 0; public String toString() { return "Student [gpa=" + gpa + ", name=" + name + "]"; } public class SortDemo { public static void main(String[] args) { ArrayList students = new ArrayList (); students.add(new Student("Jim", 3.95)); students.add(new Student("Barry", 3.98)); students.add(new Student("Doug", 3.02)); Collections.sort(students); for (Student s : students) System.out.println(s); }

Where else are interfaces used? actionPerformed actionListener mouselistener mouseClicked mouseEntered mouseExited mousePressed mouseReleased Collections – discussed soon

Can I write my own Interface? Yes! And you should! When is it appropriate? –Whenever you have a function or set of functions that are likely to be implemented in multiple ways (like compareTo) –And it’s not appropriate to use inheritance (i.e., not just overriding parent class) –Think: Would it be accurate to say a Student is-a Comparable? –NO! Comparable just ensures students can be compared.

Quick Exercise Assume you’re writing an adventure game. Lots of different game pieces move – so you might have a Moveable interface, with a method named move. With a partner, brainstorm what other behaviors might be common across completely different types of pieces. I will ask every pair for one suggestion (there may be repeats)

A few details … We know: Interface specifies common set of operations Most methods are abstract (like prototypes), no implementation. Why?* Must be public. Why? Interfaces may not have instance fields (consider: never instantiate an interface directly. Think about Comparable…) Interface may have constants. Why? Keyword: implements Class can only extend one class, but may implement as many as you want. *changed in Java 8

Interfaces in Java 8 Can now have default methods Added so that interfaces could be updated without breaking lots of existing code. In general, original philosophy (only abstract methods) will still be the best approach for many situations We won’t cover default methods make-pandas-sad-and-your-teammates-angry/

Design Decisions Should AlarmClock be an interface or an abstract class? Writing a CAD-like program. Lots of different objects (e.g., windows, walls, walkways) have an area. How to handle? Ask yourself: –Does “is-a” apply? –Are there attributes in common, or just behaviors?

Another Interface Example public interface Measureable { double getMeasure(); } public class BankAccount implements Measurable { public double getMeasure() { return balance; }... private double balance; } public class Student implements Comparable, Measurable { private String name; private double public double getMeasure() { return gpa; } // plus all the other stuff } Can implement more than one interface! keyword public required, default is package

Interface Example, continued public class DataSet { public void add(Measurable x) { sum = sum + x.getMeasure(); if (count == 0 || max.getMeasure() < x.getMeasure()) max = x; count++; } public Measureable getMaximum() { return max; } private double sum; private Measurable max; private int count; }

Interface Example, continued public static void main(String[] args) { BankAccount mine = new BankAccount(500); BankAccount yours = new BankAccount(400); DataSet data = new DataSet(); data.add(mine); data.add(yours); System.out.println (data.getMaximum().getMeasure()); DataSet data2 = new DataSet(); data2.add(new Student("Goofy", 3.9)); data2.add(new Student("Mickey Mouse", 3.0)); data2.add(new Student("Donald Duck", 2.5)); System.out.println (data2.getMaximum().getMeasure()); }

Polymorphism Measurable x = new BankAccount(1000); double m = x.getMeasure(); x = new Coin(0.1, “dime”); m = x.getMeasure(); JVM locates correct method. What did we do in C++? Overloading method – early binding (e.g., default vs 1-parameter constructor; static) Polymorphism – late binding (dynamic) How is correct method executed?

UML Diagram BankAccountCoin > Measurable DataSet stereotype indicator implements (triangular tip, dotted line) uses (open arrow tip)

javadoc comments Remember the API had a standard format. User-defined classes can easily create html documentation in that same format, by inserting comments that meet certain specifications: Start with /** First sentence describes tags specify @version) run javadoc from command line or Eclipse to generate html pages