Lecture 16 - Interfaces Professor Adams.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Interfaces A Java interface is a collection
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Abstract Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
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.
Chapter 2 Object-Oriented Design. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 2-2 Chapter Objectives Review the core concepts underlying.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Appendix B: Object-Oriented Design Java Software Structures: Designing.
Object Oriented Design and UML
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.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
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,
Interfaces and Inner Classes
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
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.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Lecture 5:Interfaces and Abstract Classes
Chapter 7 Object-Oriented Design
Chapter 5: Enhancing Classes
C# for C++ Programmers 1.
Interfaces Unit 08.
Polymorphism, Abstract Classes & Interfaces
Interfaces Professor Evan Korth.
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.
Lecture 14 - Abstract Classes
More inheritance, Abstract Classes and Interfaces
Interface.
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
Packages and Interfaces
Lecture 26: Advanced List Implementation
Interfaces.
Polymorphism, Abstract Classes & Interfaces
Lecture 14- Abstract Classes
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Chapter 5: Enhancing Classes
Advanced Java Programming
CSE 501N Fall ‘09 13: Interfaces and Multiple Representation
Java Inheritance.
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
CMSC 202 Interfaces.
Chapter 14 Abstract Classes and Interfaces
2009 Test Key.
TCSS 143, Autumn 2004 Lecture Notes
Final and Abstract Classes
Object-Oriented Design Part 2
CMSC 202 Exceptions.
CMSC 202 Interfaces.
Presentation transcript:

Lecture 16 - Interfaces Professor Adams

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 2nd Edition

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 2nd 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 Joe Weber and Mike Afergan in Using Java 2nd Edition

Interfaces A java interface is a collection of constants and abstract methods Remember: an abstract method is a method that does not have an implementation (a body) An interface can not be instantiated Although abstract methods can be preceded by the reserved word abstract, in interfaces they are generally not. Lewis & Loftus pp. 309-310

Implementation of an Interface “A class implements an interface by providing method implementations for each of the abstract methods defined in the interface.” “A class that implements an interface uses the reserved word implements followed by the class name in the class header.” If a class asserts that it implements a particular interface, it must provide a definition for all of the methods in the interface or a compile error will occur. Lewis & Loftus, p. 310

Classes that implement an interface are responsible for specifying the implementation of the methods in the interface must override every method in the interface An Example is on next slide and code is in directory.

Example and notes file is named Product.java public interface Product { static final String MAKER = "My Corp"; static final String PHONE = "555-123-4567"; 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 Attributes (need to be static because they cant be instantiated)

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 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 “An interface is similar to a class but there are several important differences: All methods in an interface are abstract; that is, they have name, parameters, and a return type but they don’t have an implementation. All methods in an interface are automatically public An interface doesn’t have instance variables.” Horstman, p. 365

Common Error with interfaces Forgetting to define implementing methods as public

Programs from text //*********************************************** Complexity.java – p. 310 * * @Author: Lewis/Loftus * The interface for an object that can be assigned an * explicit complexity. */ public interface Complexity { public void setComplexity (int complexity); public int getComplexity(); }

Interfaces A class can implement more than one interface. In these cases, the class must provide an implementation for all methods in all interfaces listed. To show that a class implements multiple interfaces, they are listed in the implements clause, separated by commas. Here is an example: Class ManyThings implements interface1, interface2,interface3 { // contains all methods of all interfaces }

Interfaces in the Java standard class library The Comparable interface is defined in java.lang and contains only one method compareTo which takes an object as a parameter and returns an integer. Documentation indicates that integer will be negative if obj1 is less than obj2; 0 if obj1 = = obj2; and positive if obj1 > obj2. Here’s an example of an invocation of the method. If (obj1.compareTo(obj2) < 0) System.out.println (“ obj1 is less than obj2”); The String class implements the Comparable interface and has a compareTo method. Lewis & Loftus, pp. 315-316

Interfaces in the Java standard class library The Iterator interface is another interface defined as part of the Java standard class library. It is used by a class that represents a collection of objects, providing a means to move through the collection one at a time. The two primary methods in the Iterator interface are: hasNext – which returns a boolean result next – which returns an object Lewis & Loftus, pp. 315-316