2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

CS18000: Problem Solving and Object-Oriented Programming.
Object Oriented Programming
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
Inheritance Inheritance Reserved word protected Reserved word super
More Inheritance Abstract Classes Interfaces Briana B. Morrison CSE 1302C Spring 2010.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Chapter 5: Enhancing Classes
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,
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Abstract Classes.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Object Oriented Design and UML
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
The Java Programming Language
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
© 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.
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.
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.
Introduction to Java Java Translation Program Structure
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
© 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.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
CSE 1201 Object Oriented Programming Abstract Classes and Interfaces.
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.
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.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Chapter 7 Object-Oriented Design
Chapter 5: Enhancing Classes
Polymorphism.
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Lecture 12 Inheritance.
Polymorphism, Abstract Classes & Interfaces
Interface.
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.
Interface.
Interfaces.
Polymorphism, Abstract Classes & Interfaces
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
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Chapter 8 Inheritance.
2009 Test Key.
Object-Oriented Design Part 2
Presentation transcript:

2/23- Interfaces Reference: Java 6.5 and 9.3

PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc

Interfaces are “Java’s substitute for C++’s feature of multiple inheritance…“ “…used when you want to define a certain functionality to be used in several classes, but are not sure exactly how this functionality will be defined by those classes…” Joe Weber and Mike Afergan in Using Java 2 nd Edition

Interfaces Provide constants Provide abstract methods Cannot be instantiated

Placing classes in an interface outlines common behavior leaves specific implementation to the classes themselves Makes using interfaces instead of classes a better choice when dealing with advanced data handling. Joe Weber and Mike Afergan in Using Java 2 nd Edition

Interfaces are underprivileged first cousins of classes define a set of methods and constants to be implemented by another object help define the behavior of an object by declaring a set of characteristics for the object cannot specify any implementation for methods An interface is used to establish, as a formal contract, a set of methods that a class will implement – Lewis and Loftus Joe Weber and Mike Afergan in Using Java 2 nd Edition

Classes that implement an interface are responsible for specifying the implementation of the methods in the interface must override every method in the interface must use the same signature for each overridden method may define other methods

Java syntax Instead of a class, when you create an interface, you define it as such: public interface Complexity And to implement an interface: public class Question implements Complexity

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 See Complexity.java (page 294)Complexity.java See Question.java (page 295)Question.java See MiniQuiz.java (page 297)MiniQuiz.java 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

UML for interfaces > Complexity MiniQuiz Question In UML, the dashed arrow with the open head shows an implementation relationship. The dashed arrow from MiniQuiz to Question shows a uses relationship.

Another Example Product.java Shoe.java Store.java Project

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

Example and notes public interface Product { static final String MAKER = "My Corp"; static final String PHONE = " "; public int getPrice (int id); } file is named Product.java getPrice is an abstract method – doesn’t require the word abstract and we will omit it. getPrice has no body (nor can it have one) When attributes (fields) are present they must be static and are final

Shoe.java public class Shoe implements Product { public int getPrice (int id) { if (id == 1) return (5); else return (10); } public String getMaker() { return (MAKER); } }

Store.java public class Store { static Shoe hightop; public static void main (String arg[] ) { init(); getInfo (hightop); orderInfo(hightop); } public static void init () { hightop = new Shoe(); } public static void getInfo (Shoe item) { System.out.println (" This Product is main by " + item.MAKER); System.out.println (" It costs $" + item.getPrice(1) + '\n'); } public static void orderInfo (Product item) { System.out.println (" To order from " + item.MAKER + " call " + item.PHONE + "."); System.out.println (" Each item costs $" + item.getPrice(1)); } } // end class Store

When you create an interface None of your methods may have a body All of your methods will be public No non-constant variables may be declared Our interfaces will all be public Interface filename must match interface name

extending Interfaces can not extend classes Interfaces can only extend other interfaces If you implement an extended interface, you must override both the methods in the new interface and the methods in the old interface

Remember these Classes implement interfaces to inherit their properties Methods in classes must have bodies unless they are abstract methods (in which case they are in abstract classes) Syntax of a method declaration in an interface is public return_value nameOfMethod (parameterList) throws ExceptionList ;

Interfaces The Java standard class library contains many helpful interfaces The Comparable interface contains an abstract method called compareTo, which is used to compare two objects The String class implements Comparable, giving us the ability to put strings in lexicographic order The Iterator interface contains methods that allow the user to move easily through a collection of objects

The Comparable Interface The Comparable interface provides a common mechanism for comparing one object to another if (obj1.compareTo(obj2) < 0) System.out.println (“obj1 is less than obj2”); The result is negative is obj1 is less that obj2, 0 if they are equal, and positive if obj1 is greater than obj2 When a programmer writes a class that implements the Comparable interface, it should follow this intent It's up to the programmer to determine what makes one object less than another

The Iterator Interface The Iterator interface provides a means of moving through a collection of objects, one at a time 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

Let’s look at some interfaces in javadocs java.sun.com