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.

Slides:



Advertisements
Similar presentations
Interfaces A Java interface is a collection
Advertisements

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.
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,
8. Arrays 8.1 Using Arrays 8.2 Reference versus Value Again 8.3 Passing Array Arguments and Returning Arrays 8.4 An Example: Simulating Rolls of the Dice.
Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
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.
Polymorphism & Interfaces
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
© 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.
Chapter 9 Polymorphism. Chapter Scope The role of polymorphism Dynamic binding Using inheritance for polymorphism Exploring Java interfaces in more detail.
COS 312 DAY 14 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress Report Over due – Next progress report is March 26 Assignment 4 Due Assignment 5.
Programming in Java (COP 2250) Lecture 14 & 15 Chengyong Yang Fall, 2005.
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.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
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.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
Introduction to Java Java Translation Program Structure
Lecture Notes – Inheritance and Polymorphism (Ch 9-10) Yonglei Tao.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
© 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.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
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.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
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.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Object-Oriented Design. Static Class Members Recall that a static method is one that can be invoked through its class name For example, the methods of.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Chapter 7 Object-Oriented Design
Chapter 5: Enhancing Classes
Polymorphism.
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Polymorphism, Abstract Classes & Interfaces
ADT’s, Collections/Generics and Iterators
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.
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:

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 An interface is used to identify a set of methods that a class will implement An abstract method is a method header without a method body, i.e. No { } An abstract method can be declared using the modifier abstract, but because all methods in an interface are abstract, it is usually left off Methods in an interface have public visibility by default

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 by itself A class implements an interface by: –using the Java reserved word implements –providing an implementation for each abstract method that is defined in the interface If a class definition says that it implements an interface, it must implement all methods defined in the interface Classes that implement an interface can also implement their own methods and they usually do

Interfaces public class CanDo implements Doable { public void doThis () { // whatever } public int doThat () { // whatever } // etc. } implements is a reserved word Each method listed in Doable must be given a definition

Interfaces In UML CanDo > Doable + doThis( ) : void + doThat( ) : int + doThis2 (value : float, ch char) void + doTheOther(num : int) : boolean + doThis( ) : void + doThat( ) : int + doThis2 (value : float, ch char) :void + doTheOther(num : int) : boolean + doNothing ( ) : void + doSomething ( ) : void Each method listed in Doable becomes a method of CanDo CanDo can have other methods of its own A “Generalization” arrow is used for “implements” (and also for “extends”) Interface box looks like a class box with stereotype >

Interfaces In addition to (or instead of) abstract methods, an interface can contain constants When a class implements an interface, it gains access to all of its defined constants

Interfaces A class can implement multiple interfaces All interface names 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 all interfaces }

Interfaces The Java standard class library contains many interface definitions that allow other classes to treat your new class as if it were instantiated using the predefined interface as a “class type” The Comparable interface contains one abstract method called compareTo, which is used to compare two objects We discussed the compareTo method of the String class in Chapter 5 The String class implements Comparable, giving us the ability to put strings in lexicographic order

The Comparable Interface Any class can implement Comparable to provide a mechanism for comparing objects of that type The value returned from compareTo should be negative is obj1 is less that obj2, 0 if they are equal, and positive if obj1 is greater than obj2 When you design a class that implements the Comparable interface, it should follow its intent if (obj1.compareTo(obj2) < 0) System.out.println ("obj1 is ” + “less than obj2");

The Comparable Interface It's up to you as the programmer to determine what makes one object less than another For example, you may define the compareTo method of an Employee class to order employees by name (alphabetically) or by employee number or any other useful way The implementation of the method can be as straightforward or as complex as needed for the situation

The Iterator Interface An iterator is an object that provides a means of processing a collection of objects one at a time An iterator is created formally by implementing the Iterator interface, which contains three methods 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

The Iterator Interface By implementing the Iterator interface, a class formally establishes that objects of that type are iterators The programmer must decide how best to implement the iterator functions Once established, the for-each version of the for loop can be used to process the items in the iterator

Interfaces as “Class Types” You could write a class that implements certain methods (such as compareTo ) without formally implementing the interface ( Comparable ) But, formally establishing the relationship between your class and an predefined interface allows Java to deal with an object of your class as if it were an object of a class corresponding to the interface

Interfaces as “Class Types” You can cast using the interface name in ( ) You can pass an object of CanDo class to a method as an object of Doable “class”. CanDo iCanDo = new CanDo();... Doable iAmDoable = iCanDo; // widening doIt(iCanDo);... public void doIt(Doable isItReallyDoable) {... // Yes, iCanDo is Doable! }

Interfaces as “Class Types” When you are using an object “cast as” one of the interfaces that it implements, you are treating this object as if it were an object of a class defined by the interface You can only access the subset of the object’s methods that are defined in the interface CanDo methods, such as doNothing(), are not accessible when a CanDo object is cast as a Doable object because they are not defined in the Doable interface

Interfaces as “Class Types” CanDo iCanDo = new CanDo(); iCanDo.doThis();// a Doable method iCanDo.doNothing();// a CanDo method // a widening conversion - no cast Doable iAmDoable = new CanDo(); // all Doable methods are available iAmDoable.doThis(); // CanDo method not accessible via Doable interface // iAmDoable.doNothing(); // would be compiler error // but it is really there - need a cast to call it ((CanDo)iAmDoable).doNothing();